error

 set<int, less<int> > s;    什么意思? 后面的 less<int> 是什么意思?

less<int> 是保证set容器里面的元素师由小到大排列的,里面元素没有重复的。

下面的程序主要是set的构造函数的练习。有错误。可不知道哪里错了。

#include<set> 
#include<iostream> 
using namespace std;
int main () 
{ 
    int ary[] = { 5,3,7,5,2,3,7,5,5,4 }; 
    set<int> s1; 
    set<int, greater<int> > s2; 
   
    for ( int i=0; i<sizeof(ary)/sizeof(int); i++ )
    { 
        s1.insert(ary[i]);
		s2.insert(ary[i]); 
	} 
	
    set<int>::iterator It = s1.begin(); 
    cout << "s1 : ";   
    while( It != s1.end()) 
        cout << *(It++) << " "; 
    cout << endl; 

    It = s2.begin(); 
    cout << "s2 : ";   
    while(It != s2.end()) 
        cout << *(It++) << " "; 
    cout << endl; 
	
    // second form of constructor 
    set<int> s3(ary,ary+3); 
    It = s3.begin(); 
    cout << "s3 : ";   
    while(It != s3.end()) 
        cout << *(It++) << " "; 
    cout << endl; 
	
    // copy constructor (predicate of s1 is important) 
	set<int, less<int> > s4(s1); 
    It = s4.begin(); 
    cout << "s4 : "; 
    while(It != s4.end()) 
		cout << *(It++) << " "; 
    cout << endl; 
	
    return 0; 
} 


CODE:


// Priority Queues are like queues, but the 
// elements inside the data structure are 
// ordered by some predicate. 
#include <iostream> 
#include <queue> 
#include <vector> 
#include <string> 
using namespace std; 

int main () 
{ 
    priority_queue<int, vector<int>, less<int> > ipq;
    ipq.push(100); 
    ipq.push(200); 
    ipq.push(300); 
	
    cout << "size of priority_queue ipq = " 
		<< ipq.size() << endl; 
	
    cout << "ipq <int,vector<int>, less<int> > = "; 
    while ( !ipq.empty() ) 
    { 
        cout << ipq.top() << " "; 
        ipq.pop(); 
    } 
    cout << endl << endl; 
	cout << "priority_queue<string,vector<string> > spq;" 
		<< endl; 
	
    priority_queue<string,vector<string> > spq; 
    for ( int i=1; i<10; i++ ) 
        spq.push(string(i,'*')); 
	
    while ( !spq.empty() ) 
    { 
        cout << spq.top() << endl; 
        spq.pop(); 
    } 
	
    return 0; 
} 
//OUTPUT: 
// size of priority_queue ipq = 3 
// ipq <sring,vector<string> > = 300 200 100 
// 
// priority_queue<string,vector<string> > spq; 
// ********* 
// ******** 
// ******* 
// ****** 
// ***** 
// **** 
// *** 
// ** 
// * 

 spq.push(string(i,'*')); 

string(i,  '*') ;  的意思是构造一个字符串,该字符创含有 i个*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值