-2

私は c++ は初めてですが、Java se/ee7 で半年の実務経験があります。

vector<string> 例に 3 つの値を入れる方法、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--;
        }
    }
}

questions[i][0] questions[i][1] questions[i][3]可能であればのようなものを使いたいです。

4

3 に答える 3

8

を持ち、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);

そして、questions[0].titleorquestions[0].answerなどを使用します。

于 2013-10-03T10:51:08.190 に答える
4

次のような構造を持たない理由:

struct question_data {
    std::string question;
    std::string answers; // this should probably be a vector<string>
    std::string correct_answer;
};

その後:

std::vector<question_data> questions;
...
for (int i = 0; i < questions.size(); i++) { // I would personally use iterator
    println(questions[i].question);
    println(questions[i].answers);

    if (readInput() == questions[i].correct_answer) ...
}
于 2013-10-03T10:53:06.127 に答える