string是c++开发中经常使用的的标准库的里类,本篇介绍string一些常的函数。
成员函数
成员函数
构造 basic_string (公开成员函数) | |
(析构函数) | 销毁字符串,若使用内部存储则解分配它 (公开成员函数) |
为字符串赋值 (公开成员函数) | |
赋值字符给字符串 (公开成员函数) |
示例代码:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void memberFunction()
{
string str1;
string str2("camel"); //构造函数初始化
//(1) operator=(const basic_string&);
str1 = str2;
cout << std::quoted(str1) << ' ' //"camel"
<< std::quoted(str2) << endl;//"camel"
//(2) operator=(basic_string&&);
str1 = std::move(str2);
cout << std::quoted(str1) << ' ' //"camel"
<< std::quoted(str2) << endl;//""或"camel"(未指定)
//(3) operator=(const char*);
str1 = "scott";
cout << std::quoted(str1) << endl; //"scott"
//(4) operator=(char);
str1 = '?';
cout << std::quoted(str1) << endl; // "?"
//(5) operator=(std::initializer_list<char>);
str1 = {'h', 'o', 'l', 'l', 'e'};
cout << std::quoted(str1) << endl; //"hello"
//(6) operator=(const T&);
str1 = 35U; //equivalent to str1 = static_cast<char>(35U);
cout << std::quoted(str1) << endl; //"#"(ASCII = 35)
//assign示例
string str;
//assign(size_type count, char ch)
str.assign(4, '$');
cout << str << endl;
string const c("scott");
// assign(basic_string const& str)
str.assign(c);
cout << c << "==" << str << endl;
// assign(basic_string const& str, size_type pos, size_type count)
str.assign(c, 0, c.length() - 1);
cout << str << endl;
// assign(basic_string&& str)
str.assign(std::string("scott ") + "camel");
cout << str << endl;
// assign(charT const* s, size_type count)
str.assign("C-style string", 7);
cout << str << endl; // "C-style"
// assign(charT const* s)
str.assign("C-style\0string");
cout << str << endl; // "C-style"
char mutable_c_str[] = "C-style string";
// assign(InputIt first, InputIt last)
str.assign(std::begin(mutable_c_str), std::end(mutable_c_str)-1);
std::cout << str << endl; // "C-style string"
// assign(std::initializer_list<charT> ilist)
str.assign({ 'C', '-', 's', 't', 'y', 'l', 'e' });
std::cout << str << endl; // "C-style"
}
int main()
{
memberFunction();
return 0;
}
运行结果:
元素访问相关的API
元素访问 | |
访问指定字符,有边界检查 (公开成员函数) | |
访问指定字符 (公开成员函数) | |
访问首字符 (公开成员函数) | |
访问最后的字符 (公开成员函数) | |
返回指向字符串首字符的指针 (公开成员函数) | |
返回字符串的不可修改的 C 字符数组版本 (公开成员函数) |
代码示例:
void elementAccess()
{
string str("message");
str.at(2) = 'y';
cout << "str===========" << str << endl;
cout << "str.at(2)=====" << str.at(2) << endl;
cout << "str[2]========" << str[2] << endl;
cout << "str.front()===" << str.front() << endl;
cout << "str.back()====" << str.back() << endl;
cout << "str.data()====" << str.data() << endl;
cout << "str.c_str()===" << str.c_str() << endl;
}
int main()
{
elementAccess();
return 0;
}
运行结果:
容量 | |
检查字符串是否为空 (公开成员函数) | |
返回字符数 (公开成员函数) | |
返回字符数的最大值 (公开成员函数) | |
保留存储 (公开成员函数) | |
返回当前对象分配的存储空间能保存的字符数量 (公开成员函数) | |
通过释放不使用内存减少内存使用 (公开成员函数) |
示例代码:
void capacityFuntion()
{
string str("camel");
cout << "str.empty()=========" << str.empty() << endl;
cout << "str.size()==========" << str.size() << endl;
cout << "str.length()========" << str.length() << endl;
cout << "str.max_size()======" << str.max_size() << endl;
cout << "str.capacity()======" << str.capacity() << endl;
str.reserve(20);
cout << "str.capacity()======" << str.capacity() << endl;
str.shrink_to_fit();
cout << "str.capacity()======" << str.capacity() << endl;
cout << "str.data()==========" << str.data() << endl;
cout << "str.c_str()=========" << str.c_str() << endl;
}
int main()
{
capacityFuntion();
return 0;
}
运行结果:
迭代器 | |
(C++11) | 返回指向起始的迭代器 (公开成员函数) |
(C++11) | 返回指向末尾的迭代器 (公开成员函数) |
(C++11) | 返回指向起始的逆向迭代器 (公开成员函数) |
(C++11) | 返回指向末尾的逆向迭代器 (公开成员函数) |
示例代码
void iteratorsFuntion()
{
string str("CameK");
cout << "str.begin()=========" << *str.begin() << endl;
cout << "str.cbegin()========" << *str.cbegin() << endl;
cout << "str.end()===========" << *(--str.end()) << endl;//最后一个元素
cout << "str.cend()==========" << *(--str.cend()) << endl;//最后一个元素
cout << "str.rbegin()========" << *str.rbegin() << endl;//最后一个元素
cout << "str.crbegin()=======" << *str.crbegin() << endl;//最后一个元素
cout << "str.rend()===========" << *(--str.rend()) << endl;//第一个元素
cout << "str.crend()==========" << *(--str.crend()) << endl;//第一个元素
}
int main()
{
iteratorsFuntion();
return 0;
}
运行结果:
参考: