C++string类的详细使用方法

String类的详细使用

文章目录

  • String类的详细使用
          • 初始化
          • 扩容空间
            • resize与reserve扩容
            • 长度获取
          • 插入与删除函数
            • 运算符插入
            • append插入
            • assign字符串截取
            • push_back尾插
            • erase删除
            • replase替换
            • swap交换
            • pop_back尾删
            • substr截断字符串
          • 功能
            • copy拷贝
            • find查找
            • rfind反向查找
            • find_first_of匹配查找查找
            • find_first_not_of不匹配查找
            • operator[ ] 和at访问
          • Iterator迭代器

初始化

C++风格

//初始化成字符串
string str("hello string");
cout << str << endl;

//初始化 10个,字符c
string str1(10,'c');
cout << str1 << endl;

//实例化-初始化
string str2(str1);
cout << str2 << endl;

//默认初始化,调用默认构造函数
string str4();

C语言风格

string str3 = "hello,string!!!";
cout << str3 << endl;
扩容空间
  1. str.clear()把字符全清了
  2. str.empty()判断str是否为空
  3. shrink_to_fit手动扩容
  4. capacity 容量大小
  5. max_size最大长度
resize与reserve扩容

C++string类的详细使用方法_第1张图片

C++string类的详细使用方法_第2张图片

容量会比自己扩容的大一些

区别就是resize会改变size 也会改变capacity ,reserve只改变capacity

长度获取
string str("hello string");
int len1 = str.size();
int len2 = str.length();
//包含\0
//   len1=12
//   len2=12
string str("hello string");
string str1 = "hello,string!!!";
str = str1;

// 输出
// hello,string!!!
插入与删除函数
运算符插入
string str("baby");
string str1 = "hello,string!!!";
string str2 = "xxxxxxx";
string str3 = str + str2;

//输出
//babyhello,string!!!


str += str2;
str += "abcdefg";
//输出
//babyxxxxxxxabcdefg
append插入
string& append (const string& str);	
string& append (const string& str, size_t subpos, size_t sublen);//(str,str下标位置,str下标后sublen个)
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
template <class InputIterator>
string& append (InputIterator first, InputIterator last);


//例:
string str("hello");
str.append("pangrams are cool", 10,2);//获取下标10,后两个字节
//输出:
// "hellore"



assign字符串截取
string& assign (const string& str);	
string& assign (const string& str, size_t subpos, size_t sublen);//str,str下标位置,截取str下标后sublen个
string& assign (const char* s);
string& assign (const char* s, size_t n);	
string& assign (size_t n, char c);	
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);


//例:
string str;
str.assign("pangrams are cool", 10,2);
//输出:
// "re"


push_back尾插

只能插入字符


string str("hello");
str.push_back('3');
//输出
//hello3

string str("hello");
str.push_back('113');
//这里会用最后一个来作为尾插数据
//输出
//hello3

erase删除
string& erase (size_t pos = 0, size_t len = npos);//(起始下标,删除len个)
iterator erase (iterator p);	
iterator erase (iterator first, iterator last);

//举例
string str("hello");
str.erase(0,3);//0位置删除3个
//输出:
//lo


replase替换
string& replace (size_t pos,  size_t len,  const string& str);
string& replace (iterator i1, iterator i2, const string& str);
string& replace (size_t pos,  size_t len,  const string& str,size_t subpos, size_t sublen);//解释
string& replace (size_t pos,  size_t len,  const char* s);
string& replace (iterator i1, iterator i2, const char* s);
string& replace (size_t pos,  size_t len,  const char* s, size_t n);
string& replace (iterator i1, iterator i2, const char* s, size_t n);
string& replace (size_t pos,  size_t len,  size_t n, char c);
string& replace (iterator i1, iterator i2, size_t n, char c);
template <class InputIterator>
string& replace (iterator i1, iterator i2,InputIterator first, InputIterator last);


//举例
string str("hello");
string str1("xxxx");
str.replace(3, 2, str1, 2, 2);//在str中下标2后取2个字符,与str下标3位置后两个进行替换
//输出:
//helxx
swap交换
string str("hello");
string str1("string");
str.swap(str1);
//输出:
//str=string
//str1=hello

pop_back尾删
string str("hello");
str.pop_back();
//输出:
//hell
substr截断字符串
string str = "Hellostring";
str.substr() //Hellostring
str.substr(0, 3) //hel
str.substr(2, 20)//llostring
str.substr(20, 2) //抛异常
功能
  1. str.c_str()string转换尾c风格字符串,因为C中没有string
  2. data():与c_str()类似,但是返回的数组不以空字符终止
copy拷贝
size_t copy (char* s, size_t len, size_t pos = 0) const;
//(目标空间,拷贝长度,起始下标)不含\0

//举例 
char s[20];
string str("hello string!!!");
size_t length = str.copy(s, 6, 5);
s[length] = '\0';
//输出
//strin 
find查找

找到返回下标,找不到返回-1 下标往后找

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;	
size_t find (char c, size_t pos = 0) const;

//举例
string str("hello string!!!");
size_t len=str.find("ell");
//输出:
//1

rfind反向查找

找到返回下标,找不到返回-1,从下标开始找,往前找

string str("hello string!!!");
size_t len=str.rfind("!",3);  
//返回-1
find_first_of匹配查找查找
size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;	
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const;

//举例
string str("Please, replace");
size_t found = str.find_first_of("aeiou");
while (found != string::npos)//npos=-1,无符号最大值
{
	str[found] = '*';
	found = str.find_first_of("aeiou", found + 1);
}
//输出:
//Pl**s*, r*pl*c*

find_last_of反向匹配查找

find_first_not_of不匹配查找

如果不匹配就输出

string str("Please, replace");
size_t found = str.find_first_not_of("aeiou");
while (found != string::npos)
{
	cout  << str[found];
	found = str.find_first_not_of("aeiou",found+1);
		
}
//输出:
//Pls, rplc

find_last_not_of反向不匹配查找

operator[ ] 和at访问
string str("hello string");
count<<str[2]<<endl;
count<<str.at(2)<<endl;
Iterator迭代器

begin开始

end结束

迭代器的使用

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;


//举例
string str = "We think in generalities, but we live in details.";
string::iterator it = str.begin();
while (it != str.end())
{
	cout << *it;
	it++;
}
//输出:We think in generalities, but we live in details.

反向迭代器

reverse_iterator rbegin();
const_reverse_iterator rbegin() const
reverse_iterator rend();
const_reverse_iterator rend() const

    
    
//举例
string str = "We think in generalities, but we live in details.";
string::reverse_iterator rit = str.rbegin();
while (rit != str.rend())
{
	cout << *rit;
	rit++;
}
//输出:.sliated ni evil ew tub ,seitilareneg ni kniht eW

你可能感兴趣的:(c++,c++,算法,java)