我是c ++的新手,但是有半年的Java se/ee7工作经验.
我想知道如何将3个值放入vector<string>
示例中:vector<string, string, string>或者只是vector<string, string>
为了避免使用3个向量.
vector<string> questions;
vector<string> answers;
vector<string> right_answer;
questions.push_back("Who is the manufacturer of Mustang?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota");
right_answer.push_back("2");
questions.push_back("Who is the manufacturer of Corvette?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota \n4. Chevrolette");
right_answer.push_back("4");
for (int i = 0; i < questions.size(); i++) {
println(questions[i]);
println(answers[i]);
if (readInput() == right_answer[i]) {
println("Good Answer.");
} else {
println("You lost. Do you want to retry? y/n");
if(readInput() == "n"){
break;
}else{
i--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用像questions[i][0] questions[i][1] questions[i][3]是否可能的东西.
您可以在以下位置拥有struct并存储它的对象vector:
struct question
{
std::string title;
std::string choices;
std::string answer;
};
// ...
question q = {"This is a question", "Choice a\nChoice b", "Choice a"};
std::vector<question> questions;
questions.push_back(q);
Run Code Online (Sandbox Code Playgroud)
然后你会使用questions[0].title或questions[0].answer等
| 归档时间: |
|
| 查看次数: |
583 次 |
| 最近记录: |