C++ string简介

本节内容主要介绍C++中的string

1. 简介

string是C++中的字符串类(字符串就是字符的序列),string其实是一个类型别名

using string = std::basic_string<char>;

头文件

#include 

2. 初始化方法

示例

#include 
#include 

template<typename T, typename Y>
void print(T& t, Y& y)
{    
    std::cout<<t<< " is: \t" <<y<<std::endl;
}

int main(int argc, char *argv[])
{
    std::string str1;
    str1 = "C++";    

    std::string str2 = str1;    //等号

    std::string str3("C++");    //小括号

    std::string str4{"C++"};    //大括号

    std::string str5(5,'a');    //5个‘a’

    std::string str6("python",2,4);   //下标从2开始,长度为4
    
    print("str1", str1);
    print("str2", str2);
    print("str3", str3);
    print("str4", str4);
    print("str5", str5);
    print("str6", str6);


    return 0;
}

输出

str1 is:        C++
str2 is:        C++
str3 is:        C++
str4 is:        C++
str5 is:        aaaaa
str6 is:        thon

3. 使用

3.1 长度计算

方法 说明
size() 长度计算
length() 长度计算
max_size() 字符串可以达到的最大长度
int main(int argc, char *argv[])
{
    std::string str1{"nothing is difficult to the man who will try."};
    std::cout<<"str1.size() size is: \t"<<str1.size()<<std::endl;
    std::cout<<"str1.length() size is: \t"<<str1.length()<<std::endl;    
    std::cout<<"str1.max_size() size is: "<<str1.max_size()<<std::endl;    

    return 0;
}

//输出
str1.size() size is:    45
str1.length() size is:  45
str1.max_size() size is: 9223372036854775807

3.2 元素访问和遍历

方法 说明
[] 或 at() 元素访问的方法,at()越界会抛出异常
auto 自动推导类型
迭代器 iterator 或 reverse_iterator 或 const_iterator (begin,cbegin,end,cend等)
int main(int argc, char *argv[])
{
    std::string str1{"nothing is difficult to the man who will try."};

    //1.下标,at()
    std::cout<<str1[4]<<std::endl;   //下标
    std::cout<<str1.at(3)<<std::endl;   //at
    std::cout<<"at is: \t\t";
    for(size_t i = 0;i<str1.size(); i++)
    {
        //std::cout<
        std::cout<<str1.at(i)<<" ";
    }
    std::cout<<std::endl;

    //2. auto
    std::cout<<"auto is: \t";
    for(auto &au : str1)
    {
        std::cout<<au<<" ";
    }
    std::cout<<std::endl;

    //3. 迭代器
    std::string::iterator it = str1.begin();
    //std::string::const_iterator it = str1.cbegin();   //返回const
    std::cout<<"iterator is: \t";
    while(it != str1.end())
    {
        std::cout<<*it<<" ";
        it++;
    }
    std::cout<<std::endl;
	
	//反序输出
    std::string::reverse_iterator rit = str1.rbegin();
    std::cout<<"reverse_iterator is: \t";
    while(rit != str1.rend())
    {
        std::cout<<*rit<<" ";
        rit++;
    }
    std::cout<<std::endl;
    

    return 0;
}

输出

i
h
at is:          n o t h i n g   i s   d i f f i c u l t   t o   t h e   m a n   w h o   w i l l   t r y .
auto is:        n o t h i n g   i s   d i f f i c u l t   t o   t h e   m a n   w h o   w i l l   t r y .
iterator is:    n o t h i n g   i s   d i f f i c u l t   t o   t h e   m a n   w h o   w i l l   t r y .
reverse_iterator is:    . y r t   l l i w   o h w   n a m   e h t   o t   t l u c i f f i d   s i   g n i h t o n

元素类型的判断
string中的每个元素时char类型

int main(int argc, char *argv[])
{
    std::string str1{"nothing is difficult to the man who will try."};    
    char ch = 'a';
    std::cout<<typeid(std::string).name()<<std::endl;
    std::cout<<typeid(str1).name()<<std::endl;
    std::cout<<typeid(char).name()<<std::endl;
    std::cout<<typeid(str1.at(0)).name()<<std::endl;
    std::cout<<typeid(ch).name()<<std::endl;    

    return 0;
}

输出

NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
c
c
c

3.3 改变长度

方法 说明
empty() 判断字符串是否为空
capacity() 空间大小(所占内存)
clear() 情况字符,size()为0,capacity()不变
reserve(n) 调整capacity()的大小,size()不变;n
resize(n) 调整size()的大小,只有n>size时,capacity()才会随之改变
int main(int argc, char *argv[])
{
    std::string str1{"nothing is difficult to the man who will try."};
    //std::string str1{"n"};

    //输出所占空间的大小
    std::cout<<"size \t"<<str1.size()<<std::endl;
    std::cout<<"capacity \t"<<str1.capacity()<<std::endl<<std::endl;
    
    //判断是否为空串,以下三种都会判空
    //std::string str2="";
    //std::string str2;
    std::string str2{};

    //std::string str2();  //这个一个函数声明,函数声明,函数声明

    if(str2.empty())
    {
        std::cout<<"empty"<<std::endl;
    }
    else
    {
        std::cout<<"not empty"<<std::endl<<std::endl;
    }

    //情况字符串
    str1.clear();
    std::cout<<"size after clear is: \t"<<str1.size()<<std::endl;
    std::cout<<"capacity after clear is: \t"<<str1.capacity()<<std::endl<<std::endl;

    //改变容量的大小(调整内存的大小),size不会改变
    str1 = "nothing is difficult to the man who will try.";
    str1.reserve(100);
    std::cout<<"(reserve)size after clear is: \t"<<str1.size()<<std::endl;
    std::cout<<"(reserve)capacity after clear is: \t"<<str1.capacity()<<std::endl;
    std::cout<<"str1 is: "<<str1<<std::endl<<std::endl;

    //10小于str1 size,该函数不生效
    str1.reserve(10);
    std::cout<<"(reserve)size after clear is: \t"<<str1.size()<<std::endl;
    std::cout<<"(reserve)capacity after clear is: \t"<<str1.capacity()<<std::endl;
    std::cout<<"str1 is: "<<str1<<std::endl<<std::endl;

    //size和容量都会扩充,size扩充的部分会被's'填充
    str2 = "nothing is difficult to the man who will try.";
    str2.resize(60, 's');
    //str2.resize(100);    //填充的部分无数据
    std::cout<<"(resize)size after clear is: \t"<<str2.size()<<std::endl;
    std::cout<<"(resize)capacity after clear is: \t"<<str2.capacity()<<std::endl;
    std::cout<<str2<<std::endl<<std::endl;

    //size变为10,容量大小不变;内容会被截取
    str2.resize(10);
    std::cout<<"(resize)size after clear is: \t"<<str2.size()<<std::endl;
    std::cout<<"(resize)capacity after clear is: \t"<<str2.capacity()<<std::endl;
    std::cout<<str2<<std::endl<<std::endl;    

    return 0;
}

输出

size    45
capacity        45

empty
size after clear is:    0
capacity after clear is:        45

(reserve)size after clear is:   45
(reserve)capacity after clear is:       100
str1 is: nothing is difficult to the man who will try.

(reserve)size after clear is:   45
(reserve)capacity after clear is:       45
str1 is: nothing is difficult to the man who will try.

(resize)size after clear is:    60
(resize)capacity after clear is:        90
nothing is difficult to the man who will try.sssssssssssssss

(resize)size after clear is:    10
(resize)capacity after clear is:        90
nothing is

3.3 原始字符串的输出

方法 说明
R 输出原始字符串
int main(int argc, char *argv[])
{
    std::string str1{"\n\t\r"};
    std::cout<<"str1 is: "<<str1<<std::endl<<std::endl;
    
    std::string str2 = R"(\n\t\r)";
    std::cout<<"str2 is: "<<str2<<std::endl;

    std::string str3{R"(\n\t\r)"};
    std::cout<<"str3 is: "<<str3<<std::endl;

    return 0;
}

输出

str1 is:


str2 is: \n\t\r
str3 is: \n\t\r

3.4 修改&&连接

方法 说明
append() 在字符串后面追加字符串
front() 头元素
compare() 比较
push_back() 插入元素(char)
pop_back() 删除元素
swap() 交换两个string的数值
“+” 连接两个string
c_str() 将string转换为const char*
data() 将string转换为const char*,c++11之后,其功能同c_str();c++11之前,末尾不会加’\0’
substr(n, m) 截取string下标从n开始的m个字符
template<typename T, typename Y>
void print(T& t, Y& y)
{    
    std::cout<<t<< " is: \t" <<y<<std::endl;
}

int main(int argc, char *argv[])
{
    std::string str1{"apple"};
    str1.append(" orange");
    print("str1 ",str1);    

    str1 = "apple";
    str1.append(" orange",3);   //追加3个字符
    print("str1 ",str1);    

    str1 = "apple";
    str1.append(" orange",2, 3);   //从下标为2的位置开始,追加3个字符
    print("str1 ",str1);     
    /*  append的函数原型
    string& append (const string& str);
    string& append (const string& str, size_t subpos, size_t sublen);
    string& append (const char* s);
    string& append (const char* s, size_t n);
    string& append (size_t n, char c);
    template    string& append (InputIterator first, InputIterator last);
    string& append (initializer_list il);
    */

    str1 = "apple";
    str1.push_back('s');    //添加字符
    print("str1 ",str1);    
    str1.pop_back();        //删除字符
    print("str1 ",str1);     

    str1 = "nothing is difficult ";
    std::string str2{"to the man who will try."};

    str1.swap(str2);    //交换两个字符串的内容
    print("str1",str1);
    print("str2",str2);
    str1.swap(str2);

    std::string str3 = str1 + str2;
    print("str3",str3);

    const char* pch = str3.c_str();   //const char*
    //const char* pch = str3.data();   //const char*
    print("pch",pch);

    /*  //需要包含头文件#include 
    char* pch1 = nullptr;
    std::strcpy(str3,pch1);
    print("pch1",pch1);
    */

    std::string str4 = str1.substr(2,7);   //从下标为2的位置,截取7个字符
    print("str4",str4);

    std::string str5 = str1.substr(2);   //从下标为2的位置,到最后
    print("str5",str5);

    return 0;
}

输出

str1  is:       apple orange
str1  is:       apple or
str1  is:       appleran
str1  is:       apples
str1  is:       apple
str1 is:        to the man who will try.
str2 is:        nothing is difficult
str3 is:        nothing is difficult to the man who will try.
pch is:         nothing is difficult to the man who will try.
str4 is:        thing i
str5 is:        thing is difficult

3.5 查找

方法 说明
find() 正向查找
rfind() 反向查找
find_first_of(str) 正向查找str中任何一个字符出现的位置,不匹配返回std::string::npos
find_last_of(str) 反向查找str中任何一个字符出现的位置
find_first_not_of(str) 正向遍历原始字符串中的元素,当该元素不在str中则返回当前位置
find_last_not_of(str) 反向遍历原始字符串中的元素,当该元素不在str中则返回当前位置
std::string::npos 常量静态成员,size_t,值为-1
template<typename T, typename Y>
void print(T& t, Y& y)
{    
    std::cout<<t<< " is: \t" <<y<<std::endl;
}

template<typename T>
void print(T& t)
{    
    std::cout<<t<<std::endl;
}

int main(int argc, char *argv[])
{    
    std::string str1{"nothing is difficult to the man who will try."};
    std::cout<< "str size is: "<<str1.size()<<std::endl;;
    //正向查找
    //auto re = str1.find("difficult");    
    size_t re = str1.find('x');
    re == std::string::npos ? print("re not find.") : print("re", re);

    //反向查找
    size_t re1 = str1.rfind("will");
    re1 == std::string::npos ? print("re1 not find.") : print("re1", re1);

    //find_first_of()
    size_t re2 = str1.find_first_of("ig");
    re2 == std::string::npos ? print("re2 not find.") : print("re2", re2);

    //find_last_of()
    size_t re3 = str1.find_last_of("ig");
    re3== std::string::npos ? print("re3 not find.") : print("re3", re3);

    //find_first_not_of
    size_t re4 = str1.find_first_not_of("nothing");
    re4 == std::string::npos ? print("re4 not find.") : print("re4", re4);

    //find_last_not_of
    size_t re5 = str1.find_last_not_of("will");
    re5 == std::string::npos ? print("re5 not find.") : print("re5", re5);   

    /*
    find函数,其它亦同
	size_t find (const string& str, size_t pos = 0) const noexcept;
    size_t find (const char* s, size_t pos = 0) const;  
    size_t find (const char* s, size_t pos, size_type n) const;
    size_t find (char c, size_t pos = 0) const noexcept;
    */    

    return 0;
}

输出

str size is: 45
re not find.
re1 is:         36
re2 is:         4
re3 is:         37
re4 is:         7
re5 is:         44

3.5 类型转换

方法 说明
atoi() string类型转换为int类型
atof() string类型转换为double类型
atol(str) string类型转换为long类型
atoll(str) string类型转换为long long类型
int main(int argc, char *argv[])
{    
    std::string str1{"121"};
    std::cout<<std::atoi(str1.c_str())<<std::endl<<std::endl;

    std::string str2{"121.121"};
    std::cout<<std::atoi(str2.c_str())<<std::endl;
    std::cout<<std::atol(str2.c_str())<<std::endl;
    std::cout<<std::atoll(str2.c_str())<<std::endl;
    std::cout<<std::atof(str2.c_str())<<std::endl<<std::endl;

    std::string str3{"121121212"};
    std::cout<<std::atoi(str3.c_str())<<std::endl;
    std::cout<<std::atol(str3.c_str())<<std::endl;
    std::cout<<std::atoll(str3.c_str())<<std::endl;
    
    return 0;
}

输出

121

121
121
121
121.121

121121212
121121212
121121212

你可能感兴趣的:(C++,c++,开发语言)