如何对 C++ 向量使用推回方法

How to use push back method for a C++ vector

int numFiles;
cout << "How many signal files are there?";
cin >> numFiles;

vector<string> signalFiles(numFiles);
vector<string> backgroundFiles(numFiles);

string backgroundFile;

for (int i=0;i<numFiles;i++){

    string singalFiles;
    cout << "Please input the name of singal file" << i << ".";
    cin >> singalFiles[i];
    signalFiles.push_back(signalFiles());

    string backgroundFiles;
    cout << "Please input the name of background file" << i << ".";
    cin >> backgroundFiles[i];
    backgroundFiles.push_back(backgroundFiles.str());

}

我将如何为给定的向量使用 push_back 方法?

我在代码中收到以下错误消息:

No member named 'str' in 'std::__1::basic_string<char>'

我很困惑为什么会收到此消息。

您的代码存在一些问题:

  1. 您正在预先分配每个 std::vectorsize(),这意味着您正在为它们预先填充空白值。然后你正在阅读那些现有的元素(这很好)并且还 push_back()ing 你已经存储的内容(这是错误的)。因此,要么停止预分配 size()(您可以使用 reserve() 方法来预分配 capacity())以便正确使用 push_back(),或者使用向量 [] 运算符分配现有元素并完全删除 push_back()

  2. 您的 backgroundFiles 字符串变量与您的 backgroundFiles 向量变量(使用相同的名称)冲突。您需要重命名它们,使它们独一无二。

  3. std::string 没有 str()operator() 成员。您声明了 std::vector 容器以容纳 std::string 元素,并且您从 std::cin 读取的值已经是 std::string 类型,因此只需按原样存储它们。

  4. 您一遍又一遍地使用 cin >> 而没有考虑换行符。 >> 运算符在遇到空格时停止读取,而不是在遇到换行符时停止读取。您应该改用 std::getline()

试试这个:

int numFiles = 0;
cout << "Please input the number of signal files:";
if (!(cin >> numFiles)) {
    // error reading the number, do something
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

vector<string> signalFiles, backgroundFiles;
signalFiles.reserve(numFiles);
backgroundFiles.reserve(numFiles);

for (int i = 0; i < numFiles; ++i) {

    string signalFile;
    cout << "Please input the name of signal file " << i+1 << ":";
    if (!getline(cin, signalFile)) {
        // error reading the string, do something
        break;
    }
    signalFiles.push_back(signalFile);

    string backgroundFile;
    cout << "Please input the name of background file " << i+1 << ":";
    if (!getline(cin, backgroundFile)) {
        // error reading the string, do something
        break;
    }
    backgroundFiles.push_back(backgroundFile);
}

或者这个:

int numFiles = 0;
cout << "Please input the number of signal files:";
if (!(cin >> numFiles)) {
    // error reading the number, do something
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

vector<string> signalFiles(numFiles);
vector<string> backgroundFiles(numFiles);

for (int i = 0; i < numFiles; ++i) {

    string signalFile;
    cout << "Please input the name of signal file " << i+1 << ":";
    if (!getline(cin, signalFile)) {
        // error reading the string, do something
        break;
    }
    signalFiles[i] = signalFile;

    string backgroundFile;
    cout << "Please input the name of background file " << i+1 << ":";
    if (!getline(cin, backgroundFile)) {
        // error reading the string, do something
        break;
    }
    backgroundFiles[i] = backgroundFile;
}

我更喜欢第一种方法,因为矢量 size() 将准确反映有多少文件被成功读取和推送。使用第二种方法,如果发生错误,向量 size() 将包含未被覆盖的默认空白值。