【问题标题】:Printing vector arrays in C++在 C++ 中打印向量数组
【发布时间】:2014-01-30 11:50:50
【问题描述】:

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

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

我需要一些函数来写下向量数组中的所有数字。我尝试用谷歌搜索,但没有任何代码适合我。

【问题讨论】:

标签: c++ arrays vector printing


【解决方案1】:
// 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;
    }
} 

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我认为发布的代码不会按预期工作,因为 j 永远不会在外部 for 循环内重置为 *i.begin()。
    • for-statement 的初始化子句中初始化迭代器会更惯用。在任何情况下,您都需要为外循环的每次迭代重新初始化 j。另外,应该是i-&gt;begin()(*i).begin(),而不是*i.begin()
    【解决方案3】:
    void Print (const vector<int>& arrays){
         for (std::vector<int>::iterator it = arrays.begin() ;
              it != arrays.end();
              ++it)
        std::cout << ' ' << *it;
    }
    

    【讨论】:

      【解决方案4】:
      #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;
      } 
      

      【讨论】:

        【解决方案5】:

        你有std::vector&lt;std::vector&lt;int&gt;&gt;,那么这个函数可能看起来像

        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&lt;int&gt;,那么它将看起来像

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

        【讨论】:

          【解决方案6】:

          这个呢?

          为 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&lt;int&gt; 以及std::vector&lt; std::vector &lt;int&gt; &gt;,如下所示:

          std::cout << arrays;
          

          【讨论】:

            【解决方案7】:

            如果你有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 <<" ";
            } 
            

            【讨论】:

              【解决方案8】:

              在 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] << ' ';
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-09-13
                • 2013-09-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-12-07
                • 2017-10-31
                • 2021-12-21
                相关资源
                最近更新 更多