当前位置:首页 » 《随便一记》 » 正文

C++ STL总结(六)string_天空中的野鸟的博客

1 人参与  2021年11月29日 12:23  分类 : 《随便一记》  评论

点击全文阅读


1.string内存结构

string不是基本容器,在CppReference中归类为字符串库,实际上是std::basic_string。底层采用的是vector的存储结构,所以内存结构与vector相同。

2.string容量操作

函数功能
bool empty()检查容器是否为空
size_t size()返回容器中元素的个数
size_t length()返回字符数,与size()等同
size_t max_size()返回容器由于系统或库的实现所限制可容纳的元素最大数量
size_t capacity()返回容器已分配空间可容纳的元素数量
void reserve(size_t new_cap)增加容器的容量,若new_cap大于capacity()则分配新存储,否则不做任何处理
void resize(size_t new_size, const T& x)重设容器的size大小,如果size变大,则以默认值填充变长的位置,如果size变小,则删除变短那部分的尾部元素
void shrink_to_fit()请求移除容器未使用的容量
void test_capacity(){
        string test;
        cout << "*************************after init****************************\n";
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after push_back****************************\n";
        test.push_back('a');
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after reserve****************************\n";
        test.reserve(20);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after resize****************************\n";
        test.resize(5);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after shrink_to_fit****************************\n";
        test.shrink_to_fit();
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

}

结果:

*************************after init****************************
empty(): 1  size(): 0  length(): 0  max_size(): 4611686018427387903  capacity(): 15
*************************after push_back****************************
empty(): 0  size(): 1  length(): 1  max_size(): 4611686018427387903  capacity(): 15
*************************after reserve****************************
empty(): 0  size(): 1  length(): 1  max_size(): 4611686018427387903  capacity(): 30
*************************after resize****************************
empty(): 0  size(): 5  length(): 5  max_size(): 4611686018427387903  capacity(): 30
*************************after shrink_to_fit****************************
empty(): 0  size(): 5  length(): 5  max_size(): 4611686018427387903  capacity(): 15

3.string构造

函数功能
string()默认构造函数,构造一个空string
string(size_t n, char ch)构造具有n个值为ch的string
string(const string &other, size_t pos, size_t count==npos)以 other 的子串 [pos, pos+count) 构造 string
string(const char* s , size_t count)以 s的前count个字符构造string
string(const char* s)以 s中的字符构造string
string( interator first, interator last )以范围 [first, last)的内容构造string
string( const string& other )拷贝构造函数,用拷贝语义构造拥有other内容的容器
string( string&& other )移动构造函数,用移动语义构造拥有other内容的容器,移动后other为empty()
string( std::initializer_list init)构造拥有initializer_list init内容的string
void test_construct(){
        cout << "*************************string()****************************\n";
        string  test;
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(10,'a')****************************\n";
        string test1(10,'a');
        for(int i=0; i<test1.size(); ++i){
                cout << test1[i] << "  ";
        }
        cout << endl;

        string test2("abcdefghijkl");
        cout << "*************************string(test2, 1,5)****************************\n";
        string test3(test2, 1 ,5);
        for(int i=0; i<test3.size(); ++i){
                cout << test3[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(s, 5)****************************\n";
        const char* s = "hello world";
        string test4(s, 5);
        for(int i=0; i<test4.size(); ++i){
                cout << test4[i] << "  ";
        }
        cout << endl;
        
        cout << "*************************string(s)****************************\n";
        string test5(s);
        for(int i=0; i<test5.size(); ++i){
                cout << test5[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(test2.begin(), test2.begin()+3)****************************\n";
        string test6(test2.begin(), test2.begin()+3);
        for(int i=0; i<test6.size(); ++i){
                cout << test6[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(test2)****************************\n";
        string test7(test2);
        for(int i=0; i<test7.size(); ++i){
                cout << test7[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(std::move(test2))****************************\n";
        string test8(std::move(test2));
        for(int i=0; i<test8.size(); ++i){
                cout << test8[i] << "  ";
        }
        cout << endl;

        cout << "*************************string{'1','2','3','4','5'}****************************\n";
        string test9{'1','2','3','4','5'};
        for(int i=0; i<test9.size(); ++i){
                cout << test9[i] << "  ";
        }
        cout << endl;
}
*************************string()****************************

*************************string(10,'a')****************************
a  a  a  a  a  a  a  a  a  a  
*************************string(test2, 1,5)****************************
b  c  d  e  f  
*************************string(s, 5)****************************
h  e  l  l  o  
*************************string(s)****************************
h  e  l  l  o     w  o  r  l  d  
*************************string(test2.begin(), test2.begin()+3)****************************
a  b  c  
*************************string(test2)****************************
a  b  c  d  e  f  g  h  i  j  k  l  
*************************string(std::move(test2))****************************
a  b  c  d  e  f  g  h  i  j  k  l  
*************************string{'1','2','3','4','5'}****************************
1  2  3  4  5  

4.string元素访问

函数功能
at()访问指定字符,同时进行越界检查
operator[]访问指定字符
front()访问第一个字符
back()访问最后一个字符
data()返回指向内存中第一个字符的指针
c_str()返回不可修改的C风格的字符数组
void test_access(){
        string test("abcdefghijkl");
        cout<< "at(2):" << test.at(2) << "  [3]:"<< test[3] << "  front:" << test.front() << "  back:" << test.back() << "  *data:" << *test.data()
                << "  c_str():" << test.c_str() << endl;
}
at(2):c  [3]:d  front:a  back:l  *data:a  c_str()abcdefghijkl

5.string元素修改

函数功能
push_back(char ch)在尾部插入新字符ch
insert(iterator pos, char ch)在pos前插入新字符ch
insert(iterator pos, size_t n, char ch)在pos前插入n个新字符ch
insert(iterator pos, iterator first, iterator last)在pos前插入来自范围 [first, last)的字符
erase(size_t index0, size_t countnpos)从index开始移除count个字符
erase(iterator pos)移除pos处的字符
erase(iterator first, iterator last)移除范围 [first, last) 的字符
append(size_t n, char ch)后附n个ch
append(const string &str)后附string str
append(const string &str, size_t pos, size_t count==npos)后附 str的子串 [pos, pos+count)
append(const char* s , size_t count)后附s的前count个字符
append(const char* s)后附字符串s
append( interator first, interator last )后附范围 [first, last)的内容
append( std::initializer_list init)后附initializer_list init的内容
operator+=( char ch)后附ch
operator+=(const string &str)后附string str
operator+=(const char* s)后附字符串s
operator+=( std::initializer_list init)后附initializer_list init的内容
compare(const string &str)与str作比较
pop_back()删除尾部字符
swap(vector& other)将内容与other交换。不在单个元素上调用任何移动、复制或交换操作
clear()清除所有元素
void test_modifier(){
        string test("12345");
        cout << "*************************push_back('a')****************************\n";
        test.push_back('a');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),'b')****************************\n";
        test.insert(test.begin(), 'b');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),3,'c')****************************\n";
        test.insert(test.begin(), 3, 'c');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),test2.begin(),test2.end())****************************\n";
        string test2("abcde");
        test.insert(test.begin(), test2.begin(), test2.end());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(1,3)****************************\n";
        test.erase(1,3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin())****************************\n";
        test.erase(test.begin());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin(),test.begin()+2)****************************\n";
        test.erase(test.begin(), test.begin()+2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(3,'a')****************************\n";
        test.append(3, 'a');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2)****************************\n";
        test.append(test2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2, 1,3)****************************\n";
        test.append(test2, 1, 3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(s, 3)****************************\n";
        const char* s = "lmnopq";
        test.append(s,3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(s)****************************\n";
        test.append(s);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2.begin(),test2.begin()+2)****************************\n";
        test.append(test2.begin(), test2.begin()+2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append({'3','2','1'})****************************\n";
        test.append({'3','2','1'});
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=('a')****************************\n";
        test += 'a';
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=(test2)****************************\n";
        test += test2;
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=(\"aaaaa\")****************************\n";
        test += "aaaaa";
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+={'3','2','1'}****************************\n";
        test +={'3','2','1'};
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************compare(test2)****************************\n";
        cout << test.compare(test2) << std::endl;

        cout << "*************************pop_back()****************************\n";
        test.pop_back();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************test.swap(test2)****************************\n";
        test.swap(test2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************clear()****************************\n";
        test.clear();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;
}

*************************push_back('a')****************************
1  2  3  4  5  a  
*************************insert(test.begin(),'b')****************************
b  1  2  3  4  5  a  
*************************insert(test.begin(),3,'c')****************************
c  c  c  b  1  2  3  4  5  a  
*************************insert(test.begin(),test2.begin(),test2.end())****************************
a  b  c  d  e  c  c  c  b  1  2  3  4  5  a  
*************************erase(1,3)****************************
a  e  c  c  c  b  1  2  3  4  5  a  
*************************erase(test.begin())****************************
e  c  c  c  b  1  2  3  4  5  a  
*************************erase(test.begin(),test.begin()+2)****************************
c  c  b  1  2  3  4  5  a  
*************************append(3,'a')****************************
c  c  b  1  2  3  4  5  a  a  a  a  
*************************append(test2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  
*************************append(test2, 1,3)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  
*************************append(s, 3)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  
*************************append(s)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  
*************************append(test2.begin(),test2.begin()+2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  
*************************append({'3','2','1'})****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  
*************************+=('a')****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  
*************************+=(test2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  
*************************+=("aaaaa")****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  
*************************+={'3','2','1'}****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  3  2  1  
*************************compare(test2)****************************
1
*************************pop_back()****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  3  2  
*************************test.swap(test2)****************************
a  b  c  d  e  
*************************clear()****************************

6.其他操作

子串等操作


点击全文阅读


本文链接:http://zhangshiyu.com/post/31395.html

字符  构造  容器  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1