深入了解C++的string,这一篇就够了

1.简介:

string是C++标准库的一个重要的部分,主要用于字符串处理,在使用时,务必添加头文件  。

2.string常用函数:

(1)构造函数:

  • 生成一个空字符串str:

    string str;

  • 拷贝构造函数,生成str的复制品:

    string s(str);

  • 将字符串str中从下标strbegin开始、长度为strlen的部分作为字符串初值:
    string s(str, strbegin,strlen)

  • 生成num个c字符的字符串:
    string s(num ,c)

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string str1;
    string str2("123456789");
    string str3("123456", 0, 3);
    string str4("0123456", 5);
    string str5(5, '1');

    cout<<str1<<endl;
    cout<<str2<<endl;
    cout<<str3<<endl;
    cout<<str4<<endl;
    cout<<str5<<endl;
}

(2)string的大小和容量:

  • 返回string对象的字符个数,下面两个的执行效果相同:

    size(); length();

  • 返回string对象最多包含的字符数,超出会抛出length_error异常:

    max_size();

  • 重新分配内存之前,string对象能包含的最大字符数:

    capacity();

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string str1="Hello World";
    string str2="123456";
    int len1=str1.length();
    int len2=str2.size();
    int len3=str1.max_size();
    int len4=str1.capacity();
    cout<<len1<<endl;
    cout<<len2<<endl;
    cout<<len2<<endl;
    cout<<len4<<endl;
}

(3)转换为char*

  • 使用c_str();方法:

    c_str();

  • cout 可直接输出 string 类的对象的内容;

  • 使用 c_str() 方法转换 string 类型到 char* 类型时,需要为char*添加 const 关键字;

  • printf() 函数不能直接打印 string 类的对象的内容,可以通过将 string 转换为 char* 类型,再使用 printf() 函数打印。

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string str1="Hello World";

    // string 转换为 char*
    const char* str2 = str1.c_str();

    cout<<str1<<endl;
    cout<<str2<<endl;
}

(4)判断是否为空:

== empty() ==

    if (str1.empty())
    {
        cout << "str1 is empty." << endl;
    }

(5)char*、char[]转换为string

实际上是将 char 、char[] 定义的字符串的首地址赋值给 string 对象

#include <string>
#include <iostream>

using namespace std;

int main()
{
    const char* psz1 = "liitdar";
    char psz2[] = "alliance";

    string str1;
    string str2;

    str1 = psz1;
    str2 = psz2;

    cout<< str1 << endl;
    cout<< str2 << endl;
}

(6)string的插入:

push_back() 和 insert()

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string str1;

    // 尾插一个字符
    str1.push_back('a');
    str1.push_back('b');
    str1.push_back('c');
    cout<<str1<<endl;

    // insert(pos,char):在制定的位置pos前插入字符char
    str1.insert(str1.begin(),'1');
    cout<<str1<<endl;

}

(7)string拼接字符串:

append() & + 操作符

#include <string>
#include <iostream>
using namespace std;

int main()
{
    // 方法一:append()
    string s1("abc");
    s1.append("def");
    cout<<s1<<endl; // s1:abcdef

    // 方法二:+ 操作符
    string s2 = "abc";
    /*s2 += "def";*/
    string s3 = "def";
    s2 += s3.c_str();
    cout<<"s2:"<<s2<<endl; // s2:abcdef
}

(8)string的大小写转换:

transform算法,配合的toupper和tolower
transform(coll.begin(), coll.end(), coll.begin(), fun);

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    string s = "ABCDEFG";

    transform(s.begin(),s.end(),s.begin(),::tolower);
    cout<<s<<endl;
    return 0;
}

(9)string的查找:find

  1. size_t find (constchar* s, size_t pos = 0) const;

` //在当前字符串的pos索引位置开始,查找子串s,返回找到的位置索引,

-1表示查找不到子串
  1. size_t find (charc, size_t pos = 0) const;

//在当前字符串的pos索引位置开始,查找字符c,返回找到的位置索引,

-1表示查找不到字符
  1. size_t rfind (constchar* s, size_t pos = npos) const;

//在当前字符串的pos索引位置开始,反向查找子串s,返回找到的位置索引,

-1表示查找不到子串
  1. size_t rfind (charc, size_t pos = npos) const;

//在当前字符串的pos索引位置开始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符

  1. size_tfind_first_of (const char* s, size_t pos = 0) const;

//在当前字符串的pos索引位置开始,查找子串s的字符,返回找到的位置索引,-1表示查找不到字符

  1. size_tfind_first_not_of (const char* s, size_t pos = 0) const;

//在当前字符串的pos索引位置开始,查找第一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到字符

  1. size_t find_last_of(const char* s, size_t pos = npos) const;

//在当前字符串的pos索引位置开始,查找最后一个位于子串s的字符,返回找到的位置索引,-1表示查找不到字符

  1. size_tfind_last_not_of (const char* s, size_t pos = npos) const;

//在当前字符串的pos索引位置开始,查找最后一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    string s("dog bird chicken bird cat");

    //字符串查找-----找到后返回首字母在字符串中的下标

    // 1. 查找一个字符串
    cout << s.find("chicken") << endl;        // 结果是:9

    // 2. 从下标为6开始找字符'i',返回找到的第一个i的下标
    cout << s.find('i',6) << endl;            // 结果是:11

    // 3. 从字符串的末尾开始查找字符串,返回的还是首字母在字符串中的下标
    cout << s.rfind("chicken") << endl;       // 结果是:9

    // 4. 从字符串的末尾开始查找字符
    cout << s.rfind('i') << endl;             // 结果是:18-------因为是从末尾开始查找,所以返回第一次找到的字符

    // 5. 在该字符串中查找第一个属于字符串s的字符
    cout << s.find_first_of("13br98") << endl;  // 结果是:4---b

    // 6. 在该字符串中查找第一个不属于字符串s的字符------先匹配dog,然后bird匹配不到,所以打印4
    cout << s.find_first_not_of("hello dog 2006") << endl; // 结果是:4
    cout << s.find_first_not_of("dog bird 2006") << endl;  // 结果是:9

    // 7. 在该字符串最后中查找第一个属于字符串s的字符
    cout << s.find_last_of("13r98") << endl;               // 结果是:19

    // 8. 在该字符串最后中查找第一个不属于字符串s的字符------先匹配t--a---c,然后空格匹配不到,所以打印21
    cout << s.find_last_not_of("teac") << endl;            // 结果是:21

}
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 待检索的字符串
    string strOutput = "|0|1|2|";
    // 需要检索的子串
    string strObj = "|1|";

    // 子串位于字符串中的位置
    size_t nLoc = strOutput.find(strObj);
    // 如果检索到子串在字符串中,则打印子串的位置
    if (nLoc != string::npos)
    {
        cout << "nLoc is: " << nLoc << endl;
    }

}

(10)string排序:

sort(s.begin(),s.end())

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{

    string s = "cdefba";
    sort(s.begin(),s.end());
    cout<<s<<endl;

}
发布了84 篇原创文章 · 获赞 5 · 访问量 3562

猜你喜欢

转载自blog.csdn.net/qq_44824148/article/details/105022114