【发布时间】:2019-05-23 20:38:29
【问题描述】:
我正在制作一个要求我输入字符的程序,我想将其存储在 vector 中声明的 struct 中。
我尝试使用char 类型和string 类型的新变量输入字符,但两者都不起作用。它在打印时给了我SIGSEGV 错误。
struct Student {
int age, standard;
vector<string> first_name;
vector<char> last_name;
};
int main() {
Student st;
string k;
cin >> st.age;
getline(cin, k);
st.first_name.push_back(k);
cout << st.age << " " << endl;
cout << "\t" << st.first_name.size() << endl;
for (unsigned int x = 0; x <= st.first_name.size(); x++) {
cout << st.first_name[x] << " ";
}
}
当输入为:
11 lwpxiteeppsacowpnbxluqpmasgnwefzcsvrjxxammuqcftzgn预期的输出是
11 lwpxiteeppsacowpnbxluqpmasgnwefzcsvrjxxammuqcftzgn但我得到了一个错误。
【问题讨论】:
-
为什么是
vector<char> last_name;?而不仅仅是std::string last_name;? -
我可以看到
vector的名字,但是当你有很多名字时,只有一个可以是第一个。
标签: c++ string struct stdvector