STL next_permutation全排列 assign 复制

来源:互联网 发布:得力考勤怎么修改数据 编辑:程序博客网 时间:2024/06/16 19:44

头文件:

#include<algorithm>


函数原型:

bool next_permutation(iterator start, iterator end);

next_permutation函数的返回值是布尔类型


例1:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<algorithm>  
  3. using namespace std;  
  4. int main(){  
  5.     string str="abc";  
  6.     while(next_permutation(str.begin(),str.end()))  
  7.         cout<<str<<endl;  
  8.     return 0;  
  9. }  

输出:



例2:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<algorithm>  
  3. #include<vector>  
  4. using namespace std;  
  5. int main(){  
  6.     vector<int> dp;  
  7.     dp.push_back(1);  
  8.     dp.push_back(2);  
  9.     dp.push_back(3);  
  10.     while(next_permutation(dp.begin(),dp.end())){  
  11.         cout<<dp[0]<<dp[1]<<dp[2]<<endl;  
  12.     }  
  13.     return 0;  
  14. }  

输出:



next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。

prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。



STL vector assign用法


vector::assign //用来构造一个vector的函数,类似于copy函数
void assign( size_type _Count, const Type& _Val);

//_Count指要构造的vector成员的个数,   _Val指成员的数值,他的类型必须与vector类型一致! 
template<class InputIterator> 
void assign( InputIterator _First, InputIterator _Last ); 
//两个指针,分别指向复制开始和结束的地方! 
EXAMPLE
// vector_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector<int> v1, v2, v3;
   vector<int>::iterator iter;

   v1.push_back(10);
   v1.push_back(20);
   v1.push_back(30);
   v1.push_back(40);
   v1.push_back(50);

   cout << "v1 = " ;
   for (iter = v1.begin(); iter != v1.end(); iter++)
       cout << *iter << " ";
   cout << endl;

   v2.assign(v1.begin(), v1.end());
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
       cout << *iter << " ";
   cout << endl;

   v3.assign(7, 4) ;
   cout << "v3 = ";
   for (iter = v3.begin(); iter != v3.end(); iter++)
       cout << *iter << " ";
   cout << endl;
}
输出结果为:
v1 = 10 20 30 40 50 
v2 = 10 20 30 40 50 
v3 = 4 4 4 4 4 4 4 

Another: http://www.cplusplus.com/reference/stl/vector/assign/

0 0
原创粉丝点击