如何在 c++ 中制作字符串数组
How to make string array in c++?
我是C++新手。我想制作一个字符串数组,其中我想保存板球队球员的名字。我想以数组的形式保存它,如下所示:
player[1]= sachin;
player[2]= john smith;
我使用了下面的程序,但出现以下错误:
错误 1错误 C2661:"std::basic_istream>::getline":没有重载函数占用 1 个参数
#include "stdafx.h"
#include <iostream>
#include<string>
int main()
{
using namespace std;
string player[3];
int i;
for (int i = 1; i <= 3; i++)
{
std::getline(cin, player);
}
}
如何像保存数字数组一样保存字符串数组。
- 将
for (int i = 1; i <= 3; i++)
替换为for (int i = 0; i <= 2;
i++) -
getline()
经常会导致错误,如果您使用的是某种版本的 Linux,它就不会简单地工作。 - 您没有正确索引数组,因为它应该
player[i]
,而不仅仅是player
。 - 如果您使用的是
using namespace std;
那么更好的声明方法是在 main 函数外部声明它。
以下代码应该执行您尝试执行的操作。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string player[3];
int i;
for (int i = 0; i < 3; i++) {
cin >> player [i];
}
}
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组