简体   繁体   English

用C ++打印向量数组

[英]Printing vector arrays in C++

I am making some testing of vectors arrays and I don't know how to print it. 我正在对向量数组进行一些测试,但我不知道如何打印它。 Here is my code: 这是我的代码:

#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");
}

What I need is some function to write down all numbers in vector arrays. 我需要的是一些函数来记录向量数组中的所有数字。 I tried to Google it but none of the code work for me. 我尝试使用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;
    }
} 

You can use vector::iterator, for instance: 您可以使用vector :: iterator,例如:

vector<vector<int>>::iterator i = arrays.begin();
vector<int>::iterator j = *i.begin();

for(;i != arrays.end(); ++i) {
  for(;j != *i.end(); ++j) {
     std::cout << *j << " ";
  }
  std::cout << "\n";
}  

What about this? 那这个呢?

Create a stream output operator for vector of T like this: 为T的向量创建一个流输出运算符,如下所示:

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const & array)
    bool seenFirst = false;
    os << "[ ";
    for (auto const & i : array)
    {
       if (!seenFirst)
       {
          seenFirst = true;
          os << i;
       }
       else
       {
          os << ", " << i;
       }
    }
    os << "]";
    return os;
}

At the end you might use it for std::vector<int> as well as for std::vector< std::vector <int> > like this: 最后,您可以将其用于std::vector<int>以及std::vector< std::vector <int> >如下所示:

std::cout << arrays;

If you have Print prototype as shown then, 如果您具有如下所示的“ Print原型,

void Print (const vector<int>& arrays){
  for(auto x:arrays)
   std::cout << x <<" ";
} 

int main(){

  for(const auto& array:arrays)
    Print(array);

  system("pause");
}

Otherwise you can combine both in one function like 否则,您可以将两者合并为一个函数,例如

void Print (const vector<vector<int>>& arrays){

  for(const auto& array:arrays)
   for(auto x:array)
    std::cout << x <<" ";
} 

As you have std::vector<std::vector<int>> then the function could look as 当您拥有std::vector<std::vector<int>>该函数可能看起来像

void Print( const std::vector<std::vector<int>> &arrays )
{
   for ( const std::vector<int> &v : arrays )
   {
      for ( int x : v ) std::cout << x << ' '; // you can include std::setw here
      std::cout << std::endl;
   }
} 

Or if you need to output only std::vector<int> then it will look as 或者,如果您只需要输出std::vector<int> ,它将看起来像

void Print( const std::vector<int> &arrays )
{
   for ( int x : arrays ) std::cout << x << ' '; // you can include std::setw here
} 

If your compiler does not support the range based for statement then you can use for example standard algorithm std::copy. 如果您的编译器不支持基于范围的for语句,则可以使用例如标准算法std :: copy。

void Print( const std::vector<int> &arrays )
{
   std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );
} 

Or you can use an ordinary loop 或者您可以使用普通循环

void Print( const std::vector<int> &arrays )
{
   for ( std::vector<int>::size_type i = 0; i < arrays.size(); i++ ) 
   {        
      std::cout << arrays[i] << ' '; // you can include std::setw here
   }
} 

Or with iterators 或使用迭代器

void Print( const std::vector<int> &arrays )
{
   for ( auto it = arrays.begin(); it != arrays.end(); ++it ) 
   {        
      std::cout << *it << ' '; // you can include std::setw here
   }
} 
void Print (const vector<int>& arrays){
     for (std::vector<int>::iterator it = arrays.begin() ;
          it != arrays.end();
          ++it)
    std::cout << ' ' << *it;
}
#include <algorithm>

vector<vector<int>>::iterator it = arrays.begin();
while ( !(it == arrays.end()) {
    std::copy( (*it).begin(), (*it).end(), 
                                   std::ostream_iterator<int>( std::cout, ","));
    ++it;
} 

In C++11`` 在C ++ 11中''

for (auto i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';

for(int i=0; i<path.size(); ++i)
std::cout << path[i] << ' ';

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

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