next_permutation:找下一个排列的函数(排列组合的排列) 第一个排列是容器自身,这个函数修改容器自身使之变成字典序中下一个排列 如果有下一个排列就返回true,没有下一个排列就返回false,最终的排列是字典序。 前提:要获得全排列,第一个排列必须是字典序中最前面的,也就是要先对第一个排列进行升序排列,因为该函数只获得按字典排序的下一个排列。
int main()
{
vector<int> s{ 1,2,3 };
do{
cout << "排列:";
for (int x : s)
cout << x << " ";
cout << endl;
}while( next_permutation(s.begin(), s.end()) );//已修改s容器,使之成为下一个排列的顺序。
return 0;
}
输出: 如果main中修改为vector<int> s{ 2,1,3 }; 输出:
访问二维vector容器的几种方法。
vector<vector<int> > re;
for (int i = 0; i < re.size(); i++)
{
for (int j = 0; j < re[i].size(); j++)
printf("re = %d ", re[i][j]);
cout << endl;
}
for (vector<int> x : re)
{
for (int y : x)
{
cout << y << " ";
}
cout << endl;
}