C++顺序容器二

本文深入探讨了forward_list容器的特殊操作,包括在特定位置插入、删除元素,以及如何改变容器大小。同时,展示了如何利用resize方法灵活调整容器容量,确保内存高效利用。

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

forward_list的特殊操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <deque>
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <list>
#include <forward_list>
using  namespace  std;
//特殊的forward_list操作
//insert_after(p,t)-在迭代器p之后的位置插入元素,返回一个指向最后一个插入元素的迭代器
//emplace_after(p,args)-在p指定的位置之后创建一个元素
//erase_after(p)-删除p指向的位置之后的元素,返回一个指向被删元素之后的元素的迭代器
int  main(){
     //forward_list的操作
     //其没有定义insert,emplace和erase操作
     //而是定义了名为insert_after,emplace_after和erase_after操作
     //forward_list还定义before_begin,他返回一个首前迭代器
     forward_list< int > flst = {0,1,2,3,4,5,6,7,8,9};
     auto  prev = flst.before_begin();   //第一个元素的前一个位置
     auto  curr = flst.begin();
     while (curr!=flst.end()){
         //删除所有奇数
         if (*curr % 2){
             curr = flst.erase_after(prev);  //删除prev指向位置之后的元素,返回一个指向被删元素之后的元素的迭代器
         } else {
             prev = curr;
             ++curr;
         }
     }
 
     for ( auto  a:flst){
         cout<<a<<endl;
     }
}

list6.cpp

改变容器的大小

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <deque>
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <list>
#include <forward_list>
using namespace std;
//capacity容量
int  main(){
     //改变容器的大小
     list< int > ilist( 10 , 51 );
     for (auto a:ilist){
         cout<<a<<endl;
     }
 
     //将五个值为0的元素添加到ilist末尾
     ilist.resize( 15 );
 
     //将15个值为-1的元素添加到ilist末尾
     ilist.resize( 25 ,- 1 );
     for (auto a:ilist){
         cout<<a<<endl;
     }
 
     //管理容量的成员函数
     //capacity();不重新分配内存空间的前提下,c可以保存多少元素
     //size();已经保存的元素的数量
     vector< int > ivec;
     cout<< "size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
 
     for (vector< int >::size_type ix= 0 ;ix!= 24 ;++ix){
         ivec.push_back(ix);
     }
     cout<< "size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
 
     //reserve成员函数
     //reserve(n);分配至少能容纳n元素的空间
     ivec.reserve( 50 );
     cout<< "重新分配空间之后\n" << "size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
 
     //添加元素用光剩余的空间
     while (ivec.size()!=ivec.capacity()){
         ivec.push_back( 0 );
     }
     cout<< "size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
 
     ivec.push_back( 989 );
     cout<< "在添加一个元素-size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
 
     //使用shrink_to_fit来要求vector将超出当前大小的多余内存退回给内存
     ivec.shrink_to_fit();  //要求归还内存
     cout<< "归还内存之后--size:" <<ivec.size()<< "  " << "capacity:" <<ivec.capacity()<<endl;
}

list7.cpp

stirng容器其他构造函数和其他一些操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <deque>
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <list>
#include <forward_list>
using  namespace  std;
int  main(){
     //string支持的其他构造函数
     const  char  *cp =  "hello wsddd!!" ;
     char  noNUll[] = { 's' , 'd' };
     string s1(cp);  //拷贝cp中的字符直到遇到空字符
     cout<<s1<<endl;
     string s2(noNUll,2);  //从noNUll中拷贝两个字符
     cout<< "s2=" <<s2<<endl;
     string s3(noNUll);   //未定义,没有空字符结束
     cout<< "s3=" <<s3<<endl;
     string s4(cp+6,5);  //从cp+6开始拷贝5个字符
     cout<< "s4=" <<s4<<endl;
 
     string s5(s1,6,5);  //从s1[6]开始拷贝5个字符
     string s6(s1,6);  //从s1[6]开始拷贝,直至s1末尾
     cout<< "s5=" <<s5<< "\n" << "s6=" <<s6<<endl;
 
     //string s7(s1,98); //错误,抛出out_of_range
 
     //substr操作
     //substr返回一个string,它是原始string的一部分或全部的拷贝
 
     string str =  "hello world!!!!" ;
     string sub1 = str.substr(0,5);
     cout<<sub1<<endl;
 
     string sub2 = str.substr(6);  //从str[6]开始直到空字符
     string sub3 = str.substr(6,11);
     //string sub5 = str.substr(18); //out_of_range
     cout<<sub2<< "\n" <<sub3<<endl;
 
 
     //append函数
     //在string末尾进行插入操作
     string c( "c++ primer" );
     c.append( "hahhaha" );
     cout<< "c=" <<c<<endl;
 
     //replace函数
     //从位置10开始,删除3个字符并插入123
     c.replace(10,3, "123" );
     cout<< "c=" <<c<<endl;
}

list8.cpp

字符串中查询操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <deque>
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <list>
#include <forward_list>
using  namespace  std;
int  main(){
 
     //string的搜索操作
 
     //find函数
     //查找指定的字符串,找到返回第一个匹配位置的下标,否则返回npos
     string name( "Annslsllls" );
     auto  pos1 = name.find( "sls" );
     cout<<pos1<<endl;   //打印3
 
     string lowercase( "sdsdsdsdsd" );
     string::size_type pos2 = lowercase.find( "cv" );
     if (pos2==string::npos){
         cout<< "没有找到" <<endl;
     } else {
         cout<<pos2<<endl;
     }
 
     /*
     其他的函数还有
     find_first_of
     find_last_of
     find_first_not_of
     find_last_not_of
     */
 
     //rfind函数
     string ssss( "sdsdsdsdsdsd" );
     cout<<ssss.rfind( "sd" )<<endl;
 
     //数值转换
     int  i = 324;
     string is = to_string(i);  //将整数i转换为字符表示形式
     cout<<is<<endl;
     double  d = stod(is);   //将字符串转换为浮点数
     cout<<d<<endl;
}


FROM:  http://my.oschina.net/xinxingegeya/blog/228639


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值