0
下面的代码生成结果输出,这是我所需要的非常好。但是我想将其中每个修改后的vector<int>
保存在另一个向量中,然后通过打印vector<vector<int>>
元素来获取输出。分配和输出向量的矢量<int>
我得到了一个模板,它提供了一个向量的格式化输出,但它无法打印vector<vector<int>>
的元素,通过取消注释代码片段来检查。
这是为什么?
如何先分配然后打印vector<vector<int>>
元素,使输出保持与现在相同?
使用C++ 14(4.9.2)
#include <iostream>
#include <vector>
using namespace std;
#define s(n) scanf("%d",&n)
template<typename T>
inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
{
bool first = true;
os << "[";
for(unsigned int i = 0; i < v.size(); i++)
{
if(!first)
os << ", ";
os << v[i];
first = false;
}
return os << "]";
}
typedef vector<int> vi;
typedef vector<vi> vvi;
int main()
{
int n = 4;
// s(n);
vi list(n, 1);
// vvi rlist;
// int count = 0;
// rlist[count++] = list;
cout << list << "\n";
for (int i = 1; i <= n; ++i)
{
for (int j = n; j >= i; --j)
{
while(list[j] == list[j-1])
{
++list[j];
cout << list << "\n";
// rlist[count++] = list;
}
}
}
return 0;
}
您可能会对KerrekSB的[Pretty-print C++ STL容器](https://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers)感兴趣。 – 2015-02-12 06:41:33
你需要用count来初始化'rlist'以便使用方括号。 http://ideone.com/XY7pdJ – zahir 2015-02-12 06:47:06
谢谢你们两位, – amarvashishth 2015-02-12 07:20:17