《STL》— STL 知识点总结

本文详细介绍了C++ STL中的STL知识点,特别是vector、set、map等容器的使用,包括如何对map进行自定义排序,如按照key的长度或value排序。此外,还探讨了输入流与输出流的基本操作,以及在流中使用控制符进行格式化输出,如设置字段宽度、精度和对齐方式。内容涵盖了ACM竞赛中STL的应用,适合C++开发者和ACMer学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++:STL标准入门汇总
http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html
stl 在 acm中的应用总结
http://www.cnblogs.com/shanyr/p/5745807.html
C++输入与输出—cout和cin的用法
http://blog.youkuaiyun.com/zhanghaotian2011/article/details/8868577
C++中string.find()函数与string::npos
http://www.cnblogs.com/web100/archive/2012/12/02/cpp-string-find-npos.html
find 函数和string :: npos 的用法
http://blog.youkuaiyun.com/stpeace/article/details/13069403
memcpy、memmove、memset、memchr、memcmp、strstr详解
http://www.cnblogs.com/jingliming/p/4737409.html
使用STL的next_permutation函数生成全排列(C++)
http://www.slyar.com/blog/stl_next_permutation.html
优先队列详解(转载)
http://www.cnblogs.com/heqinghui/archive/2013/07/30/3225407.html
STL 知识点总结
http://blog.youkuaiyun.com/liujiuxiaoshitou/article/details/76687226

C++中substr函数的用法
#include<string>
#include<iostream>
using namespace std;
main()
{
	string s("12345asdf");
	string a=s.substr(0,5);       //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾
	cout<<a<<endl;
}
输出结果为:
12345

 

一、LIST:

 

构造函数
  list<int> c0; //空链表
  list<int> c1(3); //建一个含三个默认值是0的元素的链表
  list<int> c2(5,2); //建一个含五个元素的链表,值都是2
  list<int> c4(c2); //建一个c2的copy链表
  list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。
成员函数
c.begin()      返回指向链表第一个元素的迭代器。
c.end()        返回指向链表最后一个元素之后的迭代器。
c.rbegin()      返回逆向链表的第一个元素,即c链表的最后一个数据。
c.rend()       返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
operator=      重载赋值运算符。
c.assign(n,num)   将n个num拷贝赋值给链表c。
c.assign(beg,end)  将[beg,end)区间的元素拷贝赋值给链表c。
c.back()        返回链表c的最后一个元素。
c.front()       返回链表c的第一个元素。
c.empty()         判断链表是否为空。
c.size()        返回链表c中实际元素的个数。
c.max_size()     返回链表c可能容纳的最大元素数量。
c.clear()       清除链表c中的所有元素。
c.insert(pos,num)        在pos位置插入元素num。
c.insert(pos,n,num)       在pos位置插入n个元素num。
c.insert(pos,beg,end)     在pos位置插入区间为[beg,end)的元素。
c.erase(pos)          删除pos位置的元素。
c.push_back(num)         在末尾增加一个元素。
c.pop_back()            删除末尾的元素。
c.push_front(num)        在开始位置增加一个元素。
c.pop_front()           删除第一个元素。
resize(n)     从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
resize(n,num)          从新定义链表的长度,超出原始长度部分用num代替。
c1.swap(c2);            将c1和c2交换。
swap(c1,c2);            同上。
c1.merge(c2)            合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp)        合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。
c1.splice(c1.beg,c2)           将c2连接在c1的beg位置,释放c2
c1.splice(c1.beg,c2,c2.beg)      将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素
c1.splice(c1.beg,c2,c2.beg,c2.end)   将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素
remove(num)      删除链表中匹配num的元素。
remove_if(comp)    删除条件满足的元素,参数为自定义的回调函数。
reverse()        反转链表
unique()         删除相邻的元素
c.sort()         将链表排序,默认升序
c.sort(comp)     自定义回调函数实现自定义排序
重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=

二、set

begin()    ,   返回set容器的第一个元素
end()      ,   返回set容器的最后一个元素
clear()    ,    删除set容器中的所有的元素
empty()    ,    判断set容器是否为空
max_size()   ,    返回set容器可能包含的元素最大个数
size()     ,    返回当前set容器中的元素个数
rbegin     ,   返回的值和end()相同
rend()     ,   返回的值和rbegin()相同
count()       用来查找set中某个某个键值出现的次数。这个函数在set并不	                  是很实用,因为一个键值在set只可能出现0或1次,这样就变了                                                                                     判断某一键值是否在set出现过了。
Count_if(first,end,condition)  查找满足condition(函数的形式)的次数
1. vector<int>::size_type  result1 = count_if(v1.begin(), v1.end(), greater10); bool greater10(int value)  {
2.    return value >10;  
3.}  
例子:cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl;
erase(iterator)  ,  删除定位器iterator指向的值
erase(first,second) ,删除定位器first和second之间的值
erase(key_value),    删除键值key_value的值
find()  ,         返回给定值值得定位器,如果没找到则返回end()。
insert(key_value); key_value插入到set中 ,返回值是pair<set<int>::iterator,bool>,bool标志着插入是否成功,而iterator代表插入的位置,若key_value已经在set中,则iterator表示的key_value在set中的位置。
inset(first,second); 将定位器first到second之间的元素插入到set中,返回值是void.
lower_bound(key_value),返回第一个大于等于key_value的定位器
upper_bound(key_value),返回最后一个大于等于key_value的定位器

三、vector:

at() 返回指定位置的元素 
back() 返回最末一个元素 
end() 返回最末元素的迭代器(译注:实指向最末元素的下一个位置) 
capacity() 返回vector所能容纳的元素数量(在不重新分配内存的情况下) 
clear() 清空所有元素 
empty() 判断Vector是否为空(返回true时为空) 
erase() 删除指定元素 
c.erase(beg,end)
front() 返回第一个元素 
insert() 插入元素到Vector中 
max_size() 返回Vector所能容纳元素的最大数量(上限) 
pop_back() 移除最后一个元素 
push_back() 在Vector最后添加一个元素 
rbegin() 返回Vector尾部的逆迭代器 
rend() 返回Vector起始的逆迭代器 
reserve() 设置Vector最小的元素容纳数量 
resize() 改变Vector元素数量的大小 
size() 返回Vector元素数量的大小 
swap() 交换两个Vector

四、Map

map的功能
自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
快速插入Key - Value 记录。
快速删除记录
根据Key 修改value记录。
遍历所有记录。
成员变量和成员函数
1. map最基本的构造函数;
   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;
2. map添加数据;
   map<int ,string> maplive;  
   1.maplive.insert(pair<int,string>(102,"aclive"));
   2.maplive.insert(map<int,string>::value_type(321,"hai"));
   3, maplive[112]="April";//map中最简单最常用的插入添加!
3,map中元素的查找:
   find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。        
   map<int ,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
                cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;
4,map中元素的删除:
   如果删除112;
   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;
5,map中 swap的用法:
  Map中的swap不是一个容器中的元素交换,而是两个容器交换;
  int main( )
  {
      map <int, int> m1, m2, m3;
      map <int, int>::iterator m1_Iter;
      m1.insert ( pair <int, int>  ( 1, 10 ) );
      m1.insert ( pair <int, int>  ( 2, 20 ) );
      m1.insert ( pair <int, int>  ( 3, 30 ) );
      m2.insert ( pair <int, int>  ( 10, 100 ) );
      m2.insert ( pair <int, int>  ( 20, 200 ) );
      m3.insert ( pair <int, int>  ( 30, 300 ) );
   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter->second;
      cout   << "." << endl;
   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );
   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   cout << "After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   // This is the specialized template version of swap
   swap( m1, m3 );
   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout   << "." << endl;
}
6.map的sort问题:
  Map中的元素是自动按key升序排序,所以不能对map用sort函数:
7,   map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()         返回指向map头部的迭代器
      clear()        删除所有元素
      count()         返回指定元素出现的次数
      empty()        如果map为空则返回true
      end()           返回指向map末尾的迭代器
      erase()         删除一个元素
      find()           查找一个元素
      insert()        插入元素
      key_comp() 返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()    返回可以容纳的最大元素个数
      rbegin()        返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()            返回map中元素的个数
      swap()           交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()       返回比较元素value的函数

五、Set结构体排序:

 

#include <iostream>
#include <vector>
#include <set>
using namespace std;
 
struct node
{
	char ch;
	int cost;
	node()
	{
		ch = '\0';
		cost = 0;
	}
	//把这个函数注释了,对整个程序没有任何影响
	bool operator == ( const node &rhs) const
	{
		return rhs.ch == ch;
	}
	//如果把这个程序注释了,将会出现一堆的错误,有关STL中的错误,是无法调试、无法修改的
	bool operator < (const node &rhs) const
	{
		return rhs.cost > cost;
	}
};
// 虽然我们重定义了==函数,但这是无用的,set只根据<符号来做判断
// 可以看到,set内部根据cost排序了
注意:如果set元素是一个结构体,你最好要设置你的仿函数,不然set一般默认是按第一个字段排序的,而我们的实际情况是想按序号i排序
struct ST_Message
{
public:
      ST_Message(int seq, int64_t time, string strfrom, string strto, string strinfo){
      this->seq=seq;this->time=time;this->strfrom=strfrom;this->strto=strto;this->strinfo=strinfo;}
      int seq;
      int64_t time;
      string strfrom;
      string strto;
      string strinfo;
 
      bool operator <(const ST_Message& other) const // 注意是const函数
      {
          if (seq != other.seq) // dtime按升序排序
          {
              return (seq < other.seq);
          }
          else if(time < other.time)
          {
              return (time < other.time);
          }
          else if(strcmp(strfrom.c_str(), other.strfrom.c_str()) != 0)
          {
              return (strcmp(strfrom.c_str(), other.strfrom.c_str()) < 0);
          }
          else if(strcmp(strto.c_str(), other.strto.c_str()) != 0)
          {
              return (strcmp(strto.c_str(), other.strto.c_str()) < 0);
          }
          else
          {
              return (strcmp(strinfo.c_str(), other.strinfo.c_str()) < 0);
          }
      }
};

stl中自动有序的容器map也和set有相同的应用,如果你想快速理解,那么把这篇文章中的set改成map就差不多了。

 

总之,有序的stl容器在工程中应用什么方便和广泛,但是当我们需要自己的排序的时候,可以用仿函数来设置它!

 六、C++ STL中Map的按Key排序和按Value排序

  map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value。假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择。 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key;该学生的成绩用int类型,作为value。这样一来,我们可以根据学生姓名快速的查找到他的成绩。

        但是,我们除了希望能够查询某个学生的成绩,或许还想看看整体的情况。我们想把所有同学和他相应的成绩都输出来,并且按照我们想要的顺序进行输出:比如按照学生姓名的顺序进行输出,或者按照学生成绩的高低进行输出。换句话说,我们希望能够对map进行按Key排序或按Value排序,然后按序输出其键值对的内容。

一、C++ STL中Map的按Key排序

       其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树)。在我们插入<key, value>键值对时,就会按照key的大小顺序进行存储。这也是作为key的类型必须能够进行<运算比较的原因。现在我们用string类型作为key,因此,我们的存储就是按学生姓名的字典排序储存的。

【参考代码】

1. #include<map>  

2. #include<string>  

3. #include<iostream>  

4. using

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值