【发布时间】:2014-10-26 00:51:50
【问题描述】:
我正在尝试使用字符数组输入一个单词序列。我不想使用 STL 中的字符串。我哪里错了?
int n;
cout<<"Enter the number of words:";
cin>>n;
char **s = new char*[n];
for(int i=0;i<n;i++)
{
char *s = new char[10];
cin>>s[i];
}
【问题讨论】:
-
使用
std::vector而不是自己管理内存会让您受益匪浅。像std::vector<std::vector<char>>(n, std::vector<char>(10))这样的东西。如果您存储字符串std::vector<std::vector<std::string>>(n) -
I don't want to use string from STL有什么原因吗? -
只是因为我想了解它如何与 char 一起使用。
-
我也认为你应该使用 Vector。
-
@lostboy_19
Just beacuse I want to learn how it would work with char因此,如果您想学习,请查看 SO 和其他网站上数千个正确编码的动态数组类的示例。无需将不工作的代码放在一起,然后想知道下一步该做什么。