2012-07-22 85 views
0

我想制作程序来分析程序用户写下的文本的每个字母。例如,有人输入“你好”。有没有办法将所有这些字母放在一些数组中?例如,myArray [0] = h,myArray [1] = e,... 谢谢分析用户输入C++

+1

你尝试过'cin.read()'吗? http://en.cppreference.com/w/cpp/io/basic_istream/read – timrau 2012-07-22 16:00:03

回答

6

您可以像使用数组一样使用std::string,但它是动态的,知道它的大小,并且更灵活。

//string is like a dynamic array of characters 
std::string input; 

//get a whole line of input from stdin and store it 
std::getline (std::cin, input); 

//you can manipulate it like an array, among other things 
input [0] = 'i'; //now "iello there" 

有关std::string的完整参考,请参阅here

+0

但不是std ::字符串的const char *?或者只是C – 2012-07-22 16:08:09

+0

@ColeJohnson,在内部,'std :: string'有一个'char *',它指向堆保留给你的一个区域。它通过更安全的方法封装访问,例如调整数组的大小以适合您的输入行,而不是可能超出缓冲区。 – chris 2012-07-22 16:09:55

+0

@ColeJohnson并且std :: string不是C风格的字符串都是const。 – 2012-07-22 16:10:25