c++中string类型的常用方法,以及map的使用

c++中string类型的常用方法,以及map的使用

#include <iostream>
#include <map>
using namespace std;

string str = "helloworld";
char * buf = (char *)str.data();//string转char *型

int n = 100;
string n_str = std::to_string(n);//int型转string型

string str_str = "100";
int n_int = std::stoi(str_str);//string型转int型

string test_str = "abcdefg";
for(auto n : test_str){      //遍历一个string类型
    cout << n << endl;
}

/******/
string test1_str = "aaabbbcccddd";
string::iterator it = test1_str.remove(test1_str.begin(), test1_str.end(), 'a');
cout << test1_str << endl;//输出为bbbcccdddddd
test1_str.erase(it, test1_str.end());
cout << test1_str << endl;//输出为bbbcccddd
/*
以上,remove函数为的作用为,例如,abacdda中,remove后为bcddda,返回值it则是指向第五个元素,即第三个d
使用erase之后,则删除it及以后的元素
实现删除字符串中删除所有某个特定的字符
*/

map<int, string> name;
name["1"] = "aaa";//通过键值对的方式插入,如果有1,那么则将aaa覆盖进去
name["2"] = "bbb";

string name_str = name["2"];//对map取值,不能根据值取键

for(auto i : name){    //遍历map
    cout << i.first << i.second << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值