二十一、标准库类型string的使用、string类介绍、string对象的构造和初始化、string常用成员函数
来源:互联网 发布:中国工业软件 编辑:程序博客网 时间:2024/05/21 22:54
一、string类介绍
string类型是长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
要使用string类型对象,必须包含相关头文件
#include<string>
usingstd::string;二、string对象的构造和初始化
string常用初始化例子:
#include <iostream>#include <string>using namespace std;int main(){string a1("abcdefg");cout<<a1<<endl;basic_string<char> str;string a2("abce",3);cout<<a2<<endl;string a3(a1,1,3);cout<<a3<<endl;//迭代器初始化string::iterator first = a1.begin() + 1;string::iterator last = a1.begin() + 5;string a4(first, last);//[first, last)cout<<a4<<endl;return 0;}
三、string常用的成员函数
常用的成员函数使用:
#include <string>#include <iostream>using namespace std;int main(void){string s1("abcdefdg");cout<<s1.size()<<endl;cout<<s1.length()<<endl;cout<<s1.empty()<<endl;cout<<s1.substr(1,2)<<endl;cout<<s1.substr(1)<<endl;string::size_type pos = s1.find('d', 1);if (pos == string::npos) cout<<"not found"<<endl;else cout<<"pos="<<pos<<endl;pos = s1.rfind('d');if (pos == string::npos) cout<<"not found"<<endl;else cout<<"pos="<<pos<<endl;s1.replace(2, 2, "AAAAAA");cout<<s1<<endl;s1 = "abcdefg";s1.replace(s1.begin()+1, s1.begin()+4, "AAAAAA");cout<<s1<<endl;string s2 = "xyzabc";s2.insert(2,"MMMM");cout<<s2<<endl;s2.append("6666");cout<<s2<<endl;string s3="111";s2.swap(s3);cout<<s2<<endl;cout<<s3<<endl;//find_firset_of实现的是strinfo中第一个在strset中出现的位置string strinfo=" //*---Hello World!......------";string strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";string::size_type first = strinfo.find_first_of(strset);if(first == string::npos) cout<<"not find any characters"<<endl; string::size_type last = strinfo.find_last_of(strset);if(last == string::npos)cout<<"not find any characters"<<endl;cout << strinfo.substr(first, last - first + 1)<<endl;return 0;}
0 0
- 二十一、标准库类型string的使用、string类介绍、string对象的构造和初始化、string常用成员函数
- 二十二、标准库类型vector的使用、vector介绍、vector构造和初始化、vector常用成员函数
- 标准库string对象的定义和初始化
- 标准库string对象的定义和初始化
- C++标准类string的常用函数介绍
- string类的一些常用成员函数
- string类成员函数的使用方法(二)
- String 类的成员函数
- string类的成员函数
- string类的成员函数
- string 的构造函数
- string类的构造函数
- 类String的构造函数
- string类的构造函数
- string类的构造函数
- String类型的常用
- string类型的各种构造函数
- C#----String的常用成员函数
- POJ 1050/ ZOJ 1074:To the Max - DP求子矩阵和
- linux 下tcp连接 c++ 线程处理
- novell
- JPA2.0总结
- jsp <%@include%>和<jsp:inclued />的区别
- 二十一、标准库类型string的使用、string类介绍、string对象的构造和初始化、string常用成员函数
- Python:sys模块
- opencv学习笔记-加载图像(释疑:非mat类与mat类、头文件引用)
- NSString 为什么使用copy,而不是retain?
- godot中的场景实例化上
- 常用正则表达式
- Epoll的LT模式与ET模式分析
- Python网络编程03----Python3.*中socketserver
- socket编程中write、read和send、recv之间的区别