c++ - 在 C++ 中打印 vector 数组

标签 c++ arrays vector printing

我正在对 vector 数组进行一些测试,但我不知道如何打印它。 这是我的代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include "vector"
#include <windows.h>

using namespace std;

vector<vector<int>> generateArrays(){

vector<vector<int>> cisla;

for(unsigned int i=1; i < 11; i++){
    vector<int> pole;
    vector<int> pole2;
    for(unsigned int j=0; j < i*5; j++){
        int a = rand();
        pole.push_back(a);
        pole2.push_back(a);
    }
    cisla.push_back(pole);
    cisla.push_back(pole2);
}
return cisla;
}

vector<vector<int>> arrays = generateArrays();


void Print (const vector<int>& arrays){
  // function for prinitng arrays
}  


int main(){
    Print(arrays);
  system("pause");
}

我需要的是一些函数来记下 vector 数组中的所有数字。我尝试用 Google 搜索它,但没有任何代码适合我。

最佳答案

// requires #include <algorithm> for std::copy
// requires #include <iterator> for std::ostream_iterator
void Print(const vector<vector<int>>& arrays, std::ostream& out = std::cout)
{
    for(const auto& array:arrays) {
       std::copy(array.begin(), array.end(), std::ostream_iterator<int>(out, " "));
       out << std::endl;
    }
} 

关于c++ - 在 C++ 中打印 vector 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455727/

相关文章:

c++ - 尝试将 static_cast<> const char[] 转换为 unsigned char * 时出错

c++ - 在 C++ 中使用星号拆分字符串

javascript - 热更新 JavaScript 中的特定对象

c++ - 关于 vector 的考虑

c++ - MFC CArray 中的 std::vector - 将元素添加到 CArray 后出现 "iterator not dereferencable"错误

python - 在 gensim Word2Vec 模型中匹配单词和向量

c++ - 什么更快?

c++ - C++ 中的实体系统

javascript - 将随机数存储在长度为 25 的数组中

python - 将一维结构化数组转换为二维 numpy 数组