如何创建新的字符串数组
how to create a new array of strings
我不明白这是怎么回事。Im-tring从字符串列表中创建一个字符串数组。我计算列表中的字符串数,然后想创建一个这些字符串的数组。我做了一些测试,得到了这个代码:
string *newOrder;
int numNodes;
numNodes = alphaTree.numNodes();
newOrder = new string [numNodes];
newOrder[0] = "This is";
newOrder[1] = "a test";
newOrder[2] = "To see";
newOrder[3] = "If this";
newOrder[4] = "will work";
结果是,newOrder的行为就像它是一个具有值"This is"的单个字符串数组。我做错了什么?
检查numNodes=alphaTree.numNodes();正在返回所需的大小。
下面是一段正确的代码,为5个字符串进行分配并赋值。
newOrder = new string [5];
newOrder[0] = "This is";
newOrder[1] = "a test";
newOrder[2] = "To see";
newOrder[3] = "If this";
newOrder[4] = "will work";
如果您执行以下语句:
cout << newOrder[2] << endl;
这将打印:查看
using std::string;
using std::vector;
// from an initializer_list
vector<string> newOrder1 = {
"This is",
"a test",
"To see",
"If this",
"will work",
};
// as a sequence of appends
// often used in a loop if an iterator is not applicable
vector<string> newOrder2;
newOrder2.push_back("This is");
newOrder2.push_back("a test");
newOrder2.push_back("To see");
newOrder2.push_back("If this");
newOrder2.push_back("will work");
// from an iterator-pair over any standards-compliant container
vector<string> newOrder3(alphaTree.begin(), alphaTree.end());
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组