【问题标题】:How do I print an entire vector?如何打印整个矢量?
【发布时间】: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 循环不会执行您可能希望它执行的操作。而且它不会以一种以上的方式做你可能希望它做的事情。
  • 对于打印向量,输出运算符&lt;&lt; 采用向量(或任何其他容器)没有标准重载。同样,您应该查阅课堂笔记或书籍,因为它应该包含这些信息。

标签: c++ loops for-loop vector printing


【解决方案1】:

对于初学者来说,for 语句

 for (i <= (b-a); i++;) {  

写错了。你的意思是

 for ( ;i <= (b-a); i++) {  

在这个(更新的)for循环中

 for ( ;i <= (b-a); i++) {  
    int initialvalue = a;
    listofnums[i] = initialvalue;                                 
    initialvalue++;                                               
}

向量的所有元素都具有值 a,因为变量 initialvalue 是在每次迭代中定义的。将变量的声明放在 , 循环之外。

   int initialvalue = a;
   for (i <= (b-a); i++;) {  
        listofnums[i] = initialvalue;                                 
        initialvalue++;                                               
    }

要输出向量,您可以使用例如基于范围的 for 循环

for ( const auto &item : listofnums )
{
    std::cout << item << ' ';
}
std::cout << '\n';

这是一个演示程序。

#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>

int main()
{
    int a = 0, b = 0; 

    std::cout << "Enter the first number: ";
    std::cin >> a;
    std::cout << "Enter the second number: ";
    std::cin >> b;

    std::tie( a, b ) = std::minmax( { a, b } );

    std::vector<int> listofnums( b - a + 1 ); 

    int initialvalue = a;

    for ( auto &item : listofnums ) item = initialvalue++;

    for ( const auto &item : listofnums )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    return 0;
}

它的输出可能如下所示

Enter the first number: 10
Enter the second number: 5
5 6 7 8 9 10 

【讨论】:

  • 打印出来了,谢谢!但现在它只打印零组..?
  • 我通过将我的第一个 for 循环更改为 while 循环来修复它 - 现在它可以完美运行,谢谢!
【解决方案2】:

现在可以使用了 - 来自莫斯科的 Vlad 的建议,并将第一个 for 循环更改为 while 循环:

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

int main(){
    //variables
    int a, b, i = 0; // a & b inputs, i for iterating

    //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); 
    int initialvalue = a;
    while (i <= (b-a)) {  
        listofnums[i] = initialvalue;                                 
        initialvalue++; 
        i++;
    }

    for ( const auto &item : listofnums ){
        std::cout << item << ' ';
    }

    std::cout << '\n';

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多