【发布时间】:2020-02-02 16:53:30
【问题描述】:
我希望打印向量“listofnums”中的所有项目。我尝试将“cout
#include <iostream>
#include <vector>
using namespace std;
int main(){
//variables
int a, b, i = 0; // a & b inputs, i for iterating
int likedigits = 0; //counts the number of like digits
//entering number range
cout << "Enter the first number" << endl;
cin >> a;
cout << "Enter the second number" << endl;
cin >> b;
//making a vector to contain numbers between a and b
vector<int> listofnums((b-a)+1);
for (i <= (b-a); i++;) {
int initialvalue = a;
listofnums[i] = initialvalue;
initialvalue++;
}
cout << listofnums << endl;
return 0;
}
【问题讨论】:
-
我认为你需要退后一步,阅读关于
for循环的课堂笔记或书籍。您的for循环不会执行您可能希望它执行的操作。而且它不会以一种以上的方式做你可能希望它做的事情。 -
对于打印向量,输出运算符
<<采用向量(或任何其他容器)没有标准重载。同样,您应该查阅课堂笔记或书籍,因为它应该包含这些信息。
标签: c++ loops for-loop vector printing