vector::assign std::pair 升序降序遍历

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:14   747   0

  1. vector 和 pair 的结合(pair是一种std定义的结构体)
  2. #include <iostream>
  3. #include<vector>
  4. using namespace std;
  5. typedef vector<pair<int, int> > Type_vector;
  6. int main(int argc, char *argv[])
  7. {
  8. vector<pair<int, int> > xiaoqiang;
  9. xiaoqiang.push_back(make_pair(3,4));
  10. //vector<pair<int, int> >::iterator iter;
  11. Type_vector::iterator iter;
  12. for(iter = xiaoqiang.begin(); iter!=xiaoqiang.end(); iter++)
  13. {
  14. cout<<iter->first<<" : "<<iter->second<<endl;
  15. }
  16. return 0;
  17. }

 

assign

原型:

#include <vector>
void assign( size_type num, const TYPE& val );
void assign( input_iterator start, input_iterator end );

函数assign()将区间[start, end)中的值或num个val的副本赋值给当前的vector.

此函数将销毁先前vector中的内容.

例如, 下面的代码使用assign()将10个整数值42的副本赋值给vector:

vector<int> v; 
v.assign( 10, 42 ); 
for( vector<int>::size_type i = 0; i < v.size(); i++ )
 {
cout << v[i] << " ";
}
cout << endl;

上面的代码将产生如下输出:

42 42 42 42 42 42 42 42 42 42

下面的例子展示了如何使用assign()拷贝vector:

vector<int> v1; 
for( int i = 0; i < 10; i++ ) 
{
v1.push_back( i );
}
 

vector<int> v2; 
v2.assign( v1.begin(), v1.end() ); 
 
for( vector<int>::size_type i = 0; i < v2.size(); i++ ) {
cout << v2[i] << " ";
}
cout << endl;

运行后产生如下输出:

0 1 2 3 4 5 6 7 8 9

 
可以用 less 和greater方法来配合进行处理,升序和降序。
 
如:
 
 int a[10]={5,6,7,8,9,0,1,2,3,4};
 
 vector <int> v(a, a+10);
 
 sort(v.begin(), v.end(),less<int>());//升

sort(v.begin(), v.end(),greater<int>());//降



C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现升序与降序排列呢?有两种方法,下面的例子能很好的说明: 方法1:

我们直接来看代码吧,比较简单,容易理解:
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
struct AssessTypeInfo
{
unsigned int m_uiType; //类型ID
char m_szName[64]; //类型名称
unsigned int m_uiTotal; //总分数

bool operator < (const AssessTypeInfo& rhs ) const //升序排序时必须写的函数
{
return m_uiType < rhs.m_uiType;
}
bool operator > (const AssessTypeInfo& rhs ) const //降序排序时必须写的函数
{
return m_uiType > rhs.m_uiType;
}
}
int main()
{
vector<AssessTypeInfo > ctn ;

AssessTypeInfo a1;
a1.m_uiType=1;
AssessTypeInfo a2;
a2.m_uiType=2;

AssessTypeInfo a3;
a3.m_uiType=3;

ctn.push_back(a1);
ctn.push_back(a2);
ctn.push_back(a3);
//升序排序
sort(ctn.begin(), ctn.end(),less<AssessTypeInfo>()) ; //或者sort(ctn.begin(), ctn.end()) 默认情况为升序

for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);

//降序排序
sort(ctn.begin(), ctn.end(),greater<AssessTypeInfo>()) ;

for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);


return 0 ;
}
以上方法就可以实现升序排序,输出结果为 1 2 3
降序排序结果3 2 1。
方法2 : 不修改结构体或类的定义部分,我们用函数对象来实现:
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;
struct AssessTypeInfo
{
unsigned int m_uiType; //类型ID
char m_szName[64]; //类型名称
unsigned int m_uiTotal; //总分数
};

bool lessmark(const AssessTypeInfo& s1,const AssessTypeInfo& s2)
{
return s1.m_uiType < s2.m_uiType;
}
bool greatermark(const AssessTypeInfo& s1,const AssessTypeInfo& s2)
{
return s1.m_uiType > s2.m_uiType;
}
int main()
{
vector<AssessTypeInfo > ctn ;

AssessTypeInfo a1;
a1.m_uiType=1;
AssessTypeInfo a2;
a2.m_uiType=2;

AssessTypeInfo a3;
a3.m_uiType=3;

ctn.push_back(a1);
ctn.push_back(a2);
ctn.push_back(a3);

sort(ctn.begin(), ctn.end(),lessmark) ; //升序排序


for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);

sort(ctn.begin(), ctn.end(),greatermark) ; //降序排序


return 0 ;
}

以上方法就可以实现升序排序,输出结果为 1 2 3
降序排序结果3 2 1。
方法2是一种比较简单的方法。

以上两种方法您可根据您自己的需求选择,并且以上两种方法在VC++6.0环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出,至于为什么这样使用,请参考 stl算法中sort 部分。



遍历

inputs[i].

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP