我已经有了这个
std::string str_1, str_2, str_3, str_4, str_5, str_6, str_7, str_8;
std::string str;
std::getline(std::cin,str);
std::istringstream iss(str);
iss >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8;
输入:h l l o o(在一个字符串中)
输出:str_1 = h str_2 = e str_3 = l str_4 = l str_5 = o
这就是我想要实现的目标
输入:hello(使用cin的单个字符串中)
输出:h l l o o(5个单独的字符串)
我需要输入在一个字符串beacus中用户输入varys的字符数量
cin >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8 >> endl;
不会工作beacus你必须声明所有的变量
答案 0 :(得分:1)
为什么不迭代一个字符串并像这样打印呢?
string str1;
cin >> str1;
for(int i=0; i<str1.length(); i++)
cout << str[i] << " ";
cout << endl;
答案 1 :(得分:0)
string a = "hello";
cout << a[1];
尝试迭代a [对于不同的值]
答案 2 :(得分:0)
#include <iostream>
#include <string.h>
using namespace std;
typedef struct _ARRAY_ITEMS
{
char item[10];
int iPosicion;
} ARRAY_ITEMS;
int tokens(char * string, char token, ARRAY_ITEMS * outItems){
char cItem[50];
char TempItem[50];
int x = 0,i;
ARRAY_ITEMS items;
memset(cItem, 0x00, sizeof(cItem));
memset(TempItem, 0x00, sizeof(TempItem));
sprintf( cItem,"%s", string);
for(i = 0; i <= strlen(cItem); i++){
if (cItem[i] != token){
sprintf( TempItem,"%s%c",TempItem, cItem[i]);
}else{
memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
sprintf( items.item,"%s",TempItem);
items.iPosicion = x;
memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
memset(TempItem, 0x00, sizeof(TempItem));
x++;
}
}
memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
sprintf( items.item,"%s",TempItem);
items.iPosicion = x;
memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
return x;
}
int main() {
ARRAY_ITEMS items[10];
int iLenPos = 0;
// iLenPos = tokens("01x02x03",'x', items); //split for ","
// iLenPos = tokens("01,02,03",',', items); //split for ","
iLenPos = tokens("01.02.03",'.', items); //split for "."
for ( int i = 0; i <= iLenPos; ++i)
{
printf("POSICION %d numero %s\n",items[i].iPosicion, items[i].item);
}
//print POSICION 0 numero 01
//print POSICION 1 numero 02
//print POSICION 2 numero 03
return 0;
}