C++ 程序未使用向量进入 for 循环

ars*_*ing 0 c++ for-loop vector cin

我在用vector练习,我想push_back();用for循环来push一个元素,但是程序连for循环都进不去,求救!!!

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    cout << "check ";
    int val;
    vector<char> vec;
    cout << "check " << endl << "Its in the game";
    //those two "check" were to confirm if the program is even running and is it a 
    problem while declaring the vector 

    for(int i = 0; i < vec.size(); i++){
      
      cout << "enter element for vector" << endl;
      cin >> val;
      vec.push_back(val);
      
    }
}

 
Run Code Online (Sandbox Code Playgroud)

Epi*_*lar 5

你的向量是空的。for 循环从零开始,它不小于零,因此 for 循环永远不会运行。

  • 即使它最初不是空的,每次迭代都会将向量的大小增加 1,因此循环将是一个无限循环。 (5认同)