如何在c++中创建、访问和利用字符串数组
how to create, access, utilize string arrays in c++
我是C++的新手,我想知道如何创建空的字符串数组集,到目前为止,我有这个:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numOfStudent;
char again;
do
{
cout << "How many students are in the class?";
cin >> numOfStudent;
while (numOfStudent > 30 || numOfStudent < 5)
{
cout << "The number of students must be in the range 5 - 30n";
cout << "How many students are in the class?";
cin >> numOfStudent;
}
for (int count = 1; count <= numOfStudent; count++)
cout << "Enter the full name of student " << count << endl;
cin >> // this is the part where i want to use an array to store
cout << "Here is the list of students you have entered:n";
for (int x = 1; x <= numOfStudent; x++)
cout << x << ". " // and this is the part where i want to read an array and print out
cout << "nDo you want to continue (y/n)?";
cin >> again;
} while (again == 'Y' || again == 'y');
}
我对C++真的很陌生,任何建议和帮助都将不胜感激。非常感谢。
这看起来像是一个家庭作业问题,但即使是这样,它也是非常基本的,代码答案应该有助于让你进入更复杂的主题:
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
int main()
{
int numOfStudent;
char again;
vector<string> students;
do
{
cout << "How many students are in the class?";
cin >> numOfStudent;
while (numOfStudent > 30 || numOfStudent < 5)
{
cout << "The number of students must be in the range 5 - 30n";
cout << "How many students are in the class?";
cin >> numOfStudent;
}
for (int count = 1; count <= numOfStudent; count++)
{
string name;
cout << "Enter the full name of student " << count << endl;
cin >> name; // this is the part where i want to use an array to store
students.push_back(name);
}
cout << "Here is the list of students you have entered:n";
for (int x = 1; x <= numOfStudent; x++)
cout << "Student #"<<x << " is "<<students.at(x-1)<<"."<<endl; // and this is the part where i want to read an array and print out
cout << "nDo you want to continue (y/n)?";
cin >> again;
} while (again == 'Y' || again == 'y');
}
我使用了你的代码风格,这样你就能理解我的答案。您应该研究正确的命名空间使用(通常不建议使用using namespace std
,但对于这样一个小样本来说,这是可以的)和迭代器(我使用了简单的for循环来迭代向量,但for(auto it=students.begin()...)
是实现这一点的方法。
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
int numOfStudent;
char again;
vector<string> students;
string student;
do
{
cout << "How many students are in the class?";
cin >> numOfStudent;
while (numOfStudent > 30 || numOfStudent < 5)
{
cout << "The number of students must be in the range 5 - 30n";
cout << "How many students are in the class?";
cin >> numOfStudent;
}
for (int count = 1; count <= numOfStudent; count++)
{ // all activities on the loop must be in the { }
cout << "Enter the full name of student " << count << endl;
cin >>student
students.push_back(student);
}
cout << "Here is the list of students you have entered:n";
for (int x = 1; x <= numOfStudent; x++)
cout << x << ". "<<students[i]
cout << "nDo you want to continue (y/n)?";
cin >> again;
} while (again == 'Y' || again == 'y');
}
This should work for you. If it does not work , use a vector iterator to read student names from the vector.
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组