赞
踩
目录
empty:检查 string 是否无字符,即是否 begin() == end()
size/length:返回 string 中的 CharT
元素数,即 std::distance(begin(), end()) 。
reserve:保留存储
capacity:返回当前对象分配的存储空间能保存的字符数量
代码演示:
- #include <iostream>
- #include <string>
- using namespace std;
-
- int main()
- {
- string s;
-
- //bool empty() const;
- if (s.empty()) {
- cout << "s is empty" << endl;
- }//s is empty
-
- s = "C++";
- //size_type size() const;
- cout << s.size() << endl;//3
- //size_type length() const;
- cout << s.length() << endl;//3
-
- //size_type capacity() const;
- cout << s.capacity() << endl;//15
- //void reserve( size_type new_cap = 0 );
- s.reserve(100);
- cout << s.capacity() << endl;//111
- return 0;
- }
运行结果
index
插入 count
个字符 ch
的副本。index
插入 s
所指向的空终止字符串。字符串的长度由首个空字符,通过 Traits::length(s) 确定。index
插入字符串 str
。pos
所指向的字符前插入字符 ch
。pos
所指向的元素(如果存在)前插入 count
个字符 ch
的副本。代码演示
- #include <iostream>
- using namespace std;
-
- int main()
- {
- string s;
- // insert(size_type index, size_type count, char ch)
- s.insert(0, 2, '+');
- cout << "s1:" << s << endl;
-
- // insert(size_type index, const char* s)
- s.insert(0, "C");
- cout << "s1:" << s << endl;
-
- // insert(size_type index, string const& str)
- string s2 = "~";
- s2.insert(0, s);
- cout << "s2:" << endl;
-
- // insert(const_iterator pos, char ch)
- s2.insert(s2.begin(), '>');
- cout << "s2:" << s2 << endl;
-
- // insert(const_iterator pos, size_type count, char ch)
- s2.insert(s2.begin() + s2.find_first_of('~') + 1, 2, '>');
- cout << "s2:" << s2 << endl;
-
- return 0;
- }
运行结果:
后附给顶字符ch到字符串尾
constexpr void push_back( CharT ch );
代码演示
count
个 ch
的副本str
str
的子串 [pos, pos+count)
。若请求的子串越过 string 结尾,或若 count == npos ,则后附的子串为 [pos, size())
。。s
所指向的空终止字符串。由首个空字符用 Traits::length(s) 确定字符串的长度[s, s + count)
中的字符。此范围能含有空字符代码演示
- #include <iostream>
- #include <string>
- using namespace std;
-
- int main()
- {
- string s1 = " string ";
- string str;
-
- //string& append(size_type count,CharT ch);
- str.append(3, '*');
- cout << str << endl;
-
- //string& append( const basic_string& str )
- str.append(s1);
- cout << str << endl;
-
- //string& append( const basic_string& str,
- // size_type pos, size_type count );
- str.append(s1, 1, 3);
- cout << str << endl;
-
- //string& append( const CharT* s );
- const char* s = " is wrong";
- str.append(s);
- cout << str << endl;
-
- //string& append( const CharT* s, size_type count );
- const char* c = "####";
- str.append(c, 2);
- cout << str << endl;
-
- }
运行结果:
str
。ch
。s
所指向的空终止字符串。- #include <iostream>
- using namespace std;
-
- int main()
- {
- string s1 = "I ";
- string s2 = " am ";
- const char* c = "Jack";
- //string& operator+=( const basic_string& str );
- s1 += s2;
- cout << s1 << endl;
-
- // string& operator+=( CharT ch );
- s1 += c;
- cout << s1 << endl;
-
- //string& operator+=( const CharT* s );
- s1 += '!';
- cout << s1 << endl;
-
- return 0;
- }
运行结果:
find
寻找首个等于给定字符序列的子串。搜索始于 pos
,即找到的子串必须不始于 pos
之前的位置。
str
的首个子串。[s, s+count)
的首个子串。此范围能含空字符。s
所指向的字符串的首个子串。由首个空字符,用 Traits::length(s) 确定字符串长度。ch
(由后述规则当作单字节子串)。代码演示
- #include <iostream>
- #include <string>
- using namespace std;
-
- int main()
- {
- string const s = "This is a string";
-
- //size_type find(const basic_string & str,
- // size_type pos = 0) const;
- string s2 = "str";
- cout << s.find(s2) << endl;
-
- //size_type find( const CharT* s, size_type pos, size_type count ) const;
- int n = s.find("is");
- cout << n << endl;
- n = s.find("is", 5);
- cout << n << endl;
-
- //size_type find(CharT ch, size_type pos = 0) const;
- n = s.find('a');
- cout << "'a'=" << n << endl;
-
- n = s.find('p');
- cout << "'p'=" << n << endl;
-
- return 0;
- }
运行结果
rfind:反向搜索字符串,操作和上述的find函数类似。
清除内容
如同通过执行 erase(begin(), end()) 从 string 移除所有字符。
constexpr void clear() noexcept;
代码演示:
- #include <iostream>
- #include <string>
- using namespace std;
-
- int main()
- {
- string s{ "String" };
- cout << "before:" << s << endl;
- s.clear();
- cout << "after:" << s << endl;
- cout << s.capacity() << endl;
- cout << s.size() << endl;
- return 0;
- }
运行结果:
从字符串移除指定的字符
position
的字符。[first, last)
中的字符。代码演示
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
-
- int main()
- {
- string s1 = "This is a string.";
- cout << "1:" << s1 << endl;
-
- //string& erase( size_type index = 0, size_type count = npos );
- s1.erase(7, 2);
- cout << "1:" << s1 << endl;
-
- //iterator erase( const_iterator position );
- //find(s1.begin(), s1.end(), ' ')返回查找字符的迭代器
- s1.erase(find(s1.begin(), s1.end(), ' '));
- cout << "2:" << s1 << endl;
-
- //iterator erase( const_iterator first, const_iterator last );
- s1.erase(s1.begin(), s1.end());
- cout << "3:" << s1 << endl;
- return 0;
- }
运行结果:
从字符串中移除末字符,等价于 erase(end() - 1, 1) 。若字符串为空则行为未定义。
constexpr void pop_back();
replace
以新字符串替换 [pos, pos + count)
或 [first, last)
所指示的 string 部分
resize
重设 string 大小以含 count
个字符。
若当前大小小于 count
,则后附额外的字符。
若当前大小大于 count
,则缩减 string 到为其首 count
个元素。
swap
交换 string 与 other
的内容。可能非法化所有迭代器和引用。
代码演示
- #include <iostream>
- using namespace std;
-
- int main()
- {
- string s = "This are a string";
- //string& replace(size_type pos, size_type count,
- // const string & str);
- s.replace(5, 3, "is");
- cout << s << endl;
- string s1 = "That";
-
- //string& replace( const_iterator first, const_iterator last,
- // const string& str );
- s.replace(s.begin(), s.begin()+4,s1);
- cout << s << endl;
-
- //void resize( size_type count, CharT ch );
- s.resize(10, 'a');
- cout << s << endl;
- s.resize(20, 'a');
- cout << s << endl;
-
- cout << "s:" << s << endl;
- cout << "s1:" << s1 << endl;
- //void swap(string & other);
- s.swap(s1);
- cout << "s:" << s << endl;
- cout << "s1:" << s1 << endl;
- return 0;
- }
运行结果
compare
比较两个字符序列。
返回值:
substr()
返回子串 [pos, pos+count)
。若请求的字串越过字符串的结尾,即 count
大于 size() - pos
则返回的子串为 [pos, size()]。
copy
复制子串 [pos, pos+count)
到 dest
所指向的字符串。若请求的子串越过 string 结尾,或若 count == npos ,则复制的子串为 [pos, size())
。产生的字符串不是空终止的。
代码演示
- #include <iostream>
- using namespace std;
-
- int main()
- {
- string s = "This is a cat";
-
- string s1 = "a cat";
- string s2 = "a cat";
- //int compare( const basic_string& str ) const;
- cout << "s - s1:" << s.compare(s1) << endl;
- cout << "s1 - s2:" << s1.compare(s2) << endl;
-
- //int compare( size_type pos1, size_type count1,
- // const basic_string& str ) const;
- cout << s.compare(s.find('a'), 5, s1) << endl;
-
- //string substr( size_type pos = 0, size_type count = npos ) const;
- cout << s.substr(0, 5) << endl;
-
- //size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
- char c[15];
- int n = s.copy(c, sizeof(c), 0);
- cout << n << endl;
- c[n] = '\0';
- cout << "s:" << s << endl;
- cout << "c:" << c << endl;
- }
运行结果:
本篇概述了字符串string最常用的几种操作,增删查改等。
参考网站:cppreference.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。