繁体   English   中英

用C ++打印向量数组

[英]Printing vector arrays in C++

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

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

我需要的是一些函数来记录向量数组中的所有数字。 我尝试使用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;
    }
} 

您可以使用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";
}  

那这个呢?

为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;
}

最后,您可以将其用于std::vector<int>以及std::vector< std::vector <int> >如下所示:

std::cout << arrays;

如果您具有如下所示的“ 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");
}

否则,您可以将两者合并为一个函数,例如

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

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

当您拥有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;
   }
} 

或者,如果您只需要输出std::vector<int> ,它将看起来像

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

如果您的编译器不支持基于范围的for语句,则可以使用例如标准算法std :: copy。

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

或者您可以使用普通循环

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
   }
} 

或使用迭代器

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;
} 

在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