个人兴趣网站设计,关键词优化排名怎么做,深圳创同盟科技有限公司,网站建设行业2017⭐️ string
string 是表示字符串的字符串类#xff0c;该类的接口与常规容器的接口基本一致#xff0c;还有一些额外的操作 string 的常规操作#xff0c;在使用 string 类时#xff0c;需要使用 #include string 以及 using namespace std;。
✨ 帮助文档…⭐️ string
string 是表示字符串的字符串类该类的接口与常规容器的接口基本一致还有一些额外的操作 string 的常规操作在使用 string 类时需要使用 #include string 以及 using namespace std;。
✨ 帮助文档https://cplusplus.com/reference/string/string/string/ std::string::string 构造函数constructor
序号构造函数功能1️⃣string()默认拷贝构造空的string类对象即空字符串默认第一个字符位置是\0为了兼容c2️⃣string(const string str)拷贝构造3️⃣string(const string str , size_t pos , size_t len npos)拷贝构造的重载从字符串 pos 位置开始拷贝拷贝 len 个字符4️⃣string(const char * s)使用 c_string 来初始化 string 类对象5️⃣string(const char * s , size_t n)从 s 指向的字符串中复制前 n 个字符6️⃣string(size_t n , char c)使用 n 个 c 字符来填充字符串7️⃣template class InputIterator string (InputIterator first , InputIterator last)复制一个迭代器序列的字符串 [first , last)
#include iostream
#include string
using namespace std;int main() {string s1; // 默认构造 第一个字符是 \0cout s1 endl;/*调用构造本身的写法应该是string s2(hello world);但是为什么可以这样写呢 string s2 hello world;因为当构造函数是单参的时候是支持隐式类型转换的。这里本质上先构造再拷贝构造但是编译器通常会优化为直接构造。若不希望隐式类型的转换可以在构造函数前添加 explicit 关键字*/string s2 hello world; // 使用c字符串构造一个string类对象cout s2 endl;string s3(s2); // 拷贝构造cout s3 endl;string s4(s3, 6, 5); // 拷贝构造的重载 从下标6位置开始拷贝 拷贝5个字符cout s4 endl;string s5(hello , 3); // 拷贝 hello 前3个字符cout s5 endl;string s6(10 , a); // 使用 10个 a 填充对象cout s6 endl;string s7(s6.begin() , s6.end()); // 迭代器序列初始化cout s7 endl;return 0;
}output ps npos 原型static const size_t npos -1;。npos 的访问string::npos。无符号的 -1 相当于是整型的最大值若当不传这个参数时 或者 len 大于后面剩余字符的长度那么使用默认参数 npos都是相当于 直到字符串的结束。[ 返回 ] std::string::operator 赋值重载
序号函数名称功能1️⃣string operator (const string str)用一个新的 string 对象替换当前 string 对象的内容2️⃣string operator (const char * s)用一个c的字符串替换当前 string 对象内容3️⃣string operator (char c)使用一个字符替换当前 string 对象的内容
#include iostream
#include string
using namespace std;int main() {string s1 hello world;
// 1.string temp ccc;s1 temp;cout s1 endl;
// 2.s1 hhhh;cout s1 endl;
// 3.s1 a;cout s1 endl;return 0;
}output 元素的访问
序号函数名称功能1️⃣char operator[] (size_t pos)返回当前 string 对象中 pos 位置字符的引用2️⃣const char operator[](size_t pos) const返回当前 const string 对象中 pos 位置字符的引用3️⃣char at (size_t pos)返回当前 string 对象中 pos 位置字符的引用4️⃣const char at (size_t pos) const返回当前 const string 对象中 pos 位置字符的引用5️⃣char back()返回当前 string 对象最后一个字符的引用6️⃣const char back() const返回当前 const string 对象最后一个字符的引用7️⃣char front()返回当前 string 对象第一个字符的引用8️⃣const char front() const返回当前 const string 对象第一个字符的引用
ps at 和 [] 的行为是一样的函数都会检查 pos 是否是合法位置若不是[] 是断言错误而 at 是抛异常。
ps back [xx.size() - 1]、front [0]。
#include iostream
#include string
using namespace std;int main() {const string s1 hello;for (size_t i 0; i s1.size(); i) {cout s1[i] ;}cout endl;string s2 world;for (size_t i 0; i s2.size(); i) {s2[i];cout s2[i] ;}cout endl;string s3 hello world;cout s3.back() s3[s3.size() - 1] endl;cout s3.front() s3[0] endl;cout s3.at(4) endl;return 0;
}output 元素的长度
序号函数名称功能1️⃣size_t size() const返回 string 对象实际字符的长度2️⃣size_t length() const返回 string 对象实际字符的长度
#include iostream
#include string
using namespace std;int main() {string s hello world;cout s.size() endl; // 11cout s.length() endl; // 11return 0;
}string 迭代器
序号函数名称功能1️⃣iterator begin()返回一个迭代器该迭代器指向 string 对象的第一个字符2️⃣const_iterator begin() const返回一个迭代器该迭代器指向 const string 对象的第一个字符3️⃣iterator end()返回一个迭代器该迭代器指向 string 对象最后一个实际字符的下一个位置4️⃣const_iterator end() const返回一个迭代器该迭代器指向 const string 对象最后一个实际字符的下一个位置5️⃣reverse_iterator rbegin()返回一个反向迭代器该迭代器指向 string 对象最后一个实际字符的位置6️⃣const_reverse_iterator rbegin() const返回一个反向迭代器该迭代器指向 const string 对象最后一个实际字符的位置7️⃣reverse_iterator() rend()返回一个反向迭代器该迭代器指向 string 对象第一个字符的前一个位置8️⃣const_reverse_iterator() rend() const返回一个反向迭代器该迭代器指向 const string 对象第一个字符的前一个位置
ps [ begin() , end() )、( rend() , rbegin() ]
#include iostream
#include string
using namespace std;int main() {string s hello world;for (string::iterator it s.begin(); it ! s.end(); it) {cout *it ;}// output: h e l l o w o r l dcout endl;// 自动迭代 自动判断结束// 范围for 本质上调用的也是迭代器for (auto val : s) {cout val ;}// output: h e l l o w o r l dcout endl;for (string::reverse_iterator it s.rbegin(); it ! s.rend(); it) {cout *it ;}// output: d l r o w o l l e hcout endl;// const const string s2 nihao;string::const_iterator it s2.begin();while (it ! s2.end()) {cout *it ;it;}// output: n i h a ocout endl;string::const_reverse_iterator rit s2.rbegin();while (rit ! s2.rend()) {cout *rit ;rit;}// output: o a h i nreturn 0;
}output string 对象修改
序号函数名称功能1️⃣void push_back (char c)在当前 string 对象的末尾追加一个 c 字符2️⃣string append (const string str)在当前 string 对象的末尾追加一个 const string str 对象3️⃣string append (const string str , size_t subpos , size_t sublen)在当前 string 对象的末尾追加一个 const string str 对象的子串从 subpos 位置开始拷贝 sublen 个字符过去 类似上面构造函数的 npos 用法 4️⃣string append (const char* s);在当前 string 对象的末尾追加一个 c_string 字符串5️⃣template class InputIterator string append (InputIterator first , InputIterator last) 追加一个迭代器序列的字符串 [first , last)
#include iostream
#include string
using namespace std;int main() {string s hello;s.push_back(-);cout s endl;s hello;string temp world;s.append(temp);cout s endl;string s2 hello;string temp2 world;s2.append(temp2 , 0 , 3);cout s2 endl;string s3 hello;s3.append( world);cout s3 endl;string s4 hello;s4.append(s4.begin(), s4.end());cout s4 endl;string s5 hello;s5.append(s5.rbegin() , s5.rend());cout s5 endl;return 0;
}output std::string::operator 运算符重载
序号函数名称功能6️⃣string operator (const string str);在当前 string 对象的末尾追加一个 const string str 对象7️⃣string operator (const char* s);在当前 string 对象的末尾追加一个 c_string 字符串8️⃣string operator (char c);在当前 string 对象的末尾追加一个 c 字符
#include iostream
#include string
using namespace std;int main() {string s he;s l;s l;s o ;string temp world;s temp;cout s endl;// output: hello worldreturn 0;
}元素的容量
序号函数名称功能1️⃣size_t capacity() const返回当前 string 对象的容量大小2️⃣void reserve (size_t n 0)改变当前 string 对象的容量为 n3️⃣void resize (size_t n)将当前 string 对象的 size() 调整为 n 并初始化为 \04️⃣void resize (size_t n , char c)将当前 string 对象的 size() 调整为 n 并初始化为 c5️⃣void clear();删除当前 string 对象的所有内容size() 06️⃣bool empty() const若当前 string 对象为空返回 true否则返回 false
ps reserve 是改变容量而 resize 是改变 size() 初始化当 resize 的 n 传的比 string 的大小还小则就是删除。
#include iostream
#include string
using namespace std;int main() {string s hello;cout s.capacity() endl;s.reserve(100);cout s.capacity() endl;cout s.size() endl; // 5string s2 hello world;s2.resize(5);cout s2.size() endl; // 100cout s2 endl;s2.clear();cout s2.empty() endl;return 0;
}output std::string::insert 插入 ps 需要的查文档即可效率不高很少用。 std::string::erase 删除 std::string::c_str 返回c的字符串
序号函数名称功能1️⃣const char* c_str() const返回c的字符串使用 \0 结尾 查找
序号函数名称功能1️⃣size_t find (char c , size_t pos 0) const从当前 string 对象的 pos 位置开始查找 c 字符返回这个字符第一次出现的位置否则返回 string::npos2️⃣string substr (size_t pos 0 , size_t len npos) const返回当前对象 pos 位置开始的 len 个字符的子串
#include iostream
#include string
using namespace std;int main() {string s hello world;size_t res s.find(w , 0);if (res ! string::npos) {cout s.substr(res) endl; // world}return 0;
}序号函数名称功能3️⃣size_t rfind (char c , size_t pos npos) const从当前 string 对象的 pos 位置从后向前开始查找 c 字符返回这个字符最后一次出现的位置否则返回 string::npos 其他
序号函数名称功能1️⃣istream getline (istream is , string str , char delim)输入一行字符遇到 delim 终止2️⃣istream getline (istream is , string str)输入一行字符遇到 \n 终止3️⃣string to_string (int val)返回一个 val 的 string 对象4️⃣int stoi (const string str, size_t* idx 0, int base 10)字符串转整数。 idx 通常都为 nullptrbase 代表进制
ps to_string 支持的转换类型 ps string 可以转换为的类型 #include iostream
#include string
using namespace std;int main() {int val 10;string s_val to_string(val);cout s_val endl; // 10val stoi(s_val);cout val endl; // 10return 0;
}