我正在对向量数组进行一些测试,但我不知道如何打印它。这是我的代码:
#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 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";
}
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;
}
与您的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
}
如果您的编译器不支持基于语句的范围,则可以使用例如标准算法std ::复制。
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
}
}
呢?
为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 <<" ";
}
in 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] << ' ';