简体   繁体   English

如何将 3 个元素的向量推回 C++ 中的向量?

[英]How to pushback a vector of 3 elements into a vector in C++?

I have a long vector of values, a specified user input of row/column size.我有一个很长的值向量,一个指定的行/列大小的用户输入。 I need to assign a set of 3 numbers into a vector, from the long list of vectors.我需要从一长串向量中将一组 3 个数字分配到一个向量中。 The vector with 3 number set will be pushed back into another vector, with user input row/column size.具有 3 个数字集的向量将被推回到另一个向量中,用户输入行/列大小。 1 column = the 3 number vector set and so on, until every column is filled out. 1 列 = 3 个数字向量集,依此类推,直到每一列都被填满。 I have trouble making this code (it needs to be in a loop).我在制作此代码时遇到了麻烦(它需要处于循环中)。 Any help please?请问有什么帮助吗?

The picture is an example of a 4x4 vector, with each column a vector of 3 numbers图片是一个4x4向量的例子,每列一个3个数字的向量

It sounds as if you want nested vectors, where each smaller vector inside your "long vector" represents a column of 3 values.听起来好像您想要嵌套向量,其中“长向量”中的每个较小向量代表一列 3 个值。 If so you could do so like:如果是这样,你可以这样做:

std::vector<int> columnVec = { 1, 2, 3 };
std::vector<std::vector<int>> longVector;
longVector.push_back(columnVec);

In the first line we declare a vector representing our column and place three integers inside it.在第一行中,我们声明了一个表示我们列的向量,并在其中放置了三个整数。 On line two we declare another vector, but this time containing vectors which themselves contain ints, ie a vector full of column vectors.在第二行,我们声明了另一个向量,但这次包含本身包含整数的向量,即一个充满列向量的向量。 We then used push_back() to push the column vector into our vector of vectors.然后我们使用push_back()将列向量推入我们的向量向量中。 If you needed to print the values you could do so like:如果您需要打印值,您可以这样做:

for(auto& vec : longVector) { //Walk through our vector of vectors.
  for(int value : vec) {      //Walk through our column vectors of values.
    std::cout << value;       //Print out each value of the column.
  }
  std::cout << std::endl;     //Add a newline.
}

Note that if you print them, the columns will appear as rows in the console.请注意,如果您打印它们,列将在控制台中显示为行。 If you care about the formatting in the console it will take a bit more effort and might be worth asking as a separate question.如果您关心控制台中的格式,则需要更多的努力,并且可能值得作为一个单独的问题提出。

One possible approach might look something like this:一种可能的方法可能如下所示:

#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

int main()
{
    typedef std::vector<int> VecInt;
    typedef VecInt::iterator VecIntIter;

    typedef std::vector<VecInt> VecVecInt;
    typedef VecVecInt::iterator VecVecIntIter;

    VecVecInt rows;
    const int maxRows = 10, maxCols = 10;

    cout << "Values during creation" << endl;
    cout << "----------------------" << endl;
    for (int rowNum=0; rowNum<maxRows; rowNum++)
    {
        VecInt curRow;
        for (int colNum=0; colNum<maxCols; colNum++)
        {
            if (colNum != 0)
                cout << " ";
            int cellValue = rand() % 32;
            cout << cellValue;
            curRow.push_back( cellValue );
        }
        cout << endl;
        rows.push_back(curRow);
    }

    cout << endl;
    cout << "Values during retrieval" << endl;
    cout << "----------------------" << endl;
    for (VecVecIntIter rowIter=rows.begin(); rowIter!=rows.end(); rowIter++)
    {
        VecInt curRow = (*rowIter);
        for (VecIntIter colIter=curRow.begin(); colIter!=curRow.end(); colIter++)
        {
            if (colIter != curRow.begin())
                cout << " ";
            cout << (*colIter);
        }
        cout << endl;
    }
}

Though, this will store a collection of rows, rather than a collection of columns.不过,这将存储行的集合,而不是列的集合。 Easy enough to change the for loops.很容易改变 for 循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM