快速上手STL中string类的使用

目录

一、构造函数

二、容量操作

三、元素访问

四、字符串修改

1.+=操作符重载

2.追加函数append

3.尾插

4.尾删

5.重新赋值

6.插入

7.删除

8.查找

9.交换两个字符串

五、字符串的其他操作

1.获取C风格的字符串

2.复制字符串中的指定内容

3.提取子串

4.+操作符重载

5.获取一行


一、构造函数

1、构造空的字符串(默认构造)

  • 函数原型:string();

2、拷贝构造

  • 函数原型:string (const string& str);

3、用指定字符串的部分构造

  • 函数原型:string (const string& str, size_t pos, size_t len = npos);

4、用C风格的字符串构造

  • 函数原型:string (const char* s);

5、用C风格的字符串的前n个构造

  • 函数原型:string (const char* s, size_t n);

6、用n个相同的字符构造

  • 函数原型:string (size_t n, char c);

使用示例:

#include <iostream>
#include <string>

void test_construct()
{
	// 构造空串
	std::string s1 = std::string();
	// 用C风格的字符串构造
	std::string s2("hello string");
	// 用C风格的字符串的前n个构造
	std::string s3("hello string", 5);
	// 用n个相同的字符构造
	std::string s4(5, 'h');
	// 拷贝构造
	std::string s5(s2);
	// 用指定字符串的部分构造
	std::string s6(s2, 6, 6);

	std::cout << "s1: " << s1 << std::endl;
	std::cout << "s2: " << s2 << std::endl;
	std::cout << "s3: " << s3 << std::endl;
	std::cout << "s4: " << s4 << std::endl;
	std::cout << "s5: " << s5 << std::endl;
	std::cout << "s6: " << s6 << std::endl;
}

int main()
{
	test_construct();

	return 0;
}

运行结果:
s1:
s2: hello string
s3: hello
s4: hhhhh
s5: hello string
s6: string

二、容量操作

1、获取字符串有效字符的个数

  • 函数原型:size_t size() const; size_t length() const; 这两个接口的功能是相同的,这是历史发展的原因导致的。

2、 获取字符串的容量

  • 函数原型:size_t capacity() const;

3、清空字符串

  • 函数原型:void clear();

4、判断字符串是否为空

  • 函数原型:bool empty() const;

使用示例:

void test_capacity_operation()
{
	std::string str("hello string");

	// 获取字符串有效字符的个数:size_t size() const; size_t length() const;
	size_t size = str.size();
	size_t len = str.length();

	std::cout << "size: " << size << std::endl;
	std::cout << "len: " << len << std::endl;

	// 获取字符串的容量:size_t capacity() const;
	size_t capacity = str.capacity();
	std::cout << "capacity: " << str.capacity() << std::endl;

	// 清空字符串:void clear();
	str.clear();
	std::cout << "清空字符串之后" << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl;

	// 判断字符串是否为空:bool empty() const;
	if (!str.empty())
	{
		std::cout << "字符串不为空" << std::endl;
	}
	else
	{
		std::cout << "字符串为空" << std::endl;
	}
}

int main()
{
	test_capacity_operation();

	return 0;
}

运行结果:
size: 12
len: 12
capacity: 15
清空字符串之后
size: 0
capacity: 15
字符串为空

5、改变容量

  • 函数原型:void reserve (size_t n = 0);

使用示例:

// 改变容量
void test_reserve()
{
	std::string str("hello string");

	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;

	str.reserve(100);
	std::cout << "after str.reserve(100);" << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;


	str.reserve(20);
	std::cout << "after str.reserve(20);" << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl;
}

int main()
{
	test_reserve();

	return 0;
}

运行结果:
size: 12
capacity: 15

after str.reserve(100);
size: 12
capacity: 111

after str.reserve(20);
size: 12
capacity: 111
  • 该结果为VS2019运行结果,可以看到,VS2019的结果是不缩容。具体缩不缩容还得看具体的编译器。
  • reserve只影响容量,不影响有效字符的个数。

6、改变有效字符的大小

  • 函数原型:
  • void resize (size_t n);
  • void resize (size_t n, char c);

使用示例:

void test_resize()
{
	// 测试void resize (size_t n);
	std::string str("hello string");

	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;
	
	str.resize(5);
	std::cout << "after resize(5) " << std::endl;
	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;

	str.resize(20);
	std::cout << "after resize(20) " << std::endl;
	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;

	// 测试void resize (size_t n, char c);
	// 重新为str赋值
	str = "hello string";

	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;

	str.resize(5,'X');
	std::cout << "after resize(5,'X') " << std::endl;
	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;

	str.resize(20,'Y');
	std::cout << "after resize(20,'Y') " << std::endl;
	std::cout << "str: " << str << std::endl;
	std::cout << "size: " << str.size() << std::endl;
	std::cout << "capacity: " << str.capacity() << std::endl << std::endl;
}

int main()
{
	test_resize();

	return 0;
}

运行结果:
str: hello string
size: 12
capacity: 15

after resize(5)
str: hello
size: 5
capacity: 15

after resize(20)
str: hello
size: 20
capacity: 31

str: hello string
size: 12
capacity: 31

after resize(5,'X')
str: hello
size: 5
capacity: 31

after resize(20,'Y')
str: helloYYYYYYYYYYYYYYY
size: 20
capacity: 31
  • resize既改变容量,又改变有效字符的个数。
  • 当n大于当前字符串的长度时,会用指定的字符填充剩余的有效字符的空间,如果没有指定字符,则默认使用 '\0'填充

三、元素访问

1、通过重载的operator[]访问元素

  • 函数原型:
  • char& operator[] (size_t pos);
  • const char& operator[] (size_t pos) const;

该函数重载了两个版本,一个适用于普通对象,一个适用于const对象。

普通对象调用非const版本,返回非const修饰对象的引用,可读可写。

const对象调用const版本,返回const修饰对象的引用,只可以读。

后面所有的重载const和非const版本的函数都是这个原因。

2、通过at()函数访问

  • 函数原型:
  • char& at (size_t pos);
  • const char& at (size_t pos) const;

3、通过迭代器访问

  • std::string::iterator 是定义在string类里面的类型,是STL提供的一种统一的访问容器的方式。
  • 用起来像指针

4、通过范围for访问

使用示例:

void test_element_access()
{
	std::string str("hello string");

	//1、通过重载的operator[]访问元素
	for (int i = 0; i < str.size(); ++i)
	{
		std::cout << str[i];
	}
	std::cout << std::endl;

	//2、通过at()函数访问
	for (int i = 0; i < str.size(); ++i)
	{
		std::cout << str.at(i);
	}
	std::cout << std::endl;

	//3、通过迭代器访问
	std::string::iterator begin_it = str.begin();
	std::string::iterator end_it = str.end();
	while (begin_it != end_it)
	{
		std::cout << *begin_it;
		begin_it++;
	}
	std::cout << std::endl;

	//4、通过范围for访问
	for (auto e : str)
	{
		std::cout << e;
	}
	std::cout << std::endl;
}

int main()
{
	test_element_access();

	return 0;
}

运行结果:
hello string
hello string
hello string
hello string

四、字符串修改

1.+=操作符重载

函数原型:

  • += 一个字符串:string& operator+= (const string& str);
  • += 一个C风格的字符串:string& operator+= (const char* s);
  • += 一个字符:string& operator+= (char c);

使用示例:

void test_modify_1()
{	
	std::string str1("hello ");
	std::string str2("world");

	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	//+= 一个字符串:string& operator+= (const string & str);
	str1 += str2;
	std::cout << "after -> str1 += str2" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	//+= 一个C风格的字符串:string & operator+= (const char* s);
	str1 += " and C++";
	std::cout << "after -> str1 += \"and C++\"" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	//+= 一个字符:string & operator+= (char c);
	str1 += '!';
	std::cout << "after -> str1 += \'!\'" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;
}

int main()
{
	test_modify_1();

	return 0;
}

运行结果:
str1: hello
str2: world

after -> str1 += str2
str1: hello world

after -> str1 += "and C++"
str1: hello world and C++

after -> str1 += '!'
str1: hello world and C++!

2.追加函数append

函数原型:

  • 追加一个字符串:string& append(const string & str);
  • 追加一个字符串的子串:string& append(const string & str, size_t subpos, size_t sublen);
  • 追加一个C风格的字符串:string & append(const char* s);
  • 追加C风格字符串的前n个:string& append(const char* s, size_t n);
  • 追加n个相同的字符串:string& append(size_t n, char c);

使用示例:

void test_modify_2()
{
	std::string str1("hello");
	std::string str2(" world");

	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	// 追加一个字符串:string& append(const string & str);
	str1.append(str2);
	std::cout << "after -> str1.append(str2)" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	// 追加一个字符串的子串:string& append(const string & str, size_t subpos, size_t sublen);
	str1.append(str2,0,6);
	std::cout << "after -> str1.append(str2,0,3)" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	// 追加一个C风格的字符串:string & append(const char* s);
	str2.append(" peace");
	std::cout << "after -> str2.append(\"peace\")" << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	// 追加C风格字符串的前n个:string& append(const char* s, size_t n);
	str1.append("yes or no",3);
	std::cout << "after -> str1.append(\"yes or no\",3)" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	// 追加n个相同的字符串:string& append(size_t n, char c);
	str1.append(5, 'Y');
	std::cout << "after -> str1.str1.append(5, 'Y')" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;
}

int main()
{
	test_modify_2();

	return 0;
}

运行结果:
str1: hello
str2:  world

after -> str1.append(str2)
str1: hello world

after -> str1.append(str2,0,3)
str1: hello world world

after -> str2.append("peace")
str2:  world peace

after -> str1.append("yes or no",3)
str1: hello world worldyes

after -> str1.str1.append(5, 'Y')
str1: hello world worldyesYYYYY

3.尾插

函数原型:

  • 尾插一个字符:void push_back (char c);

使用示例:

void test_modify_3()
{
	std::string str("prisin");
	std::cout << "str: " << str << std::endl << std::endl;

	// 尾插一个字符:void push_back (char c);
	str.push_back('g');
	std::cout << "after -> str.push_back('g')" << std::endl;
	std::cout << "str: " << str << std::endl;
}

int main()
{
	test_modify_3();

	return 0;
}

运行结果:
str: prisin

after -> str.push_back('g')
str: prising

4.尾删

函数原型:

  • void pop_back();

使用示例:

void test_pop_back()
{
	std::string str("hello");
	std::cout << "str: " << str << std::endl; 

	str.pop_back();
	std::cout << "str: " << str << std::endl;
}

int main()
{
	test_pop_back();

	return 0;
}

运行结果:
str: hello
str: hell

5.重新赋值

函数原型:

  • 用字符串重新赋值:string& assign(const string & str);
  • 用一个字符串的子串重新赋值:string& assign(const string & str, size_t subpos, size_t sublen);
  • 用C风格的字符串重新赋值:string & assign(const char* s);
  • 用C风格的字符串的前n个重新赋值:string& assign(const char* s, size_t n);
  • 用n个相同字符重新赋值:string& assign(size_t n, char c);

使用示例:

void test_modify_4()
{
	std::string str1("hello world");
	std::string str2("hello string");

	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	//用字符串重新赋值:string& assign(const string & str);
	str1.assign(str2);
	std::cout << "after -> str1.assign(str2)" << std::endl;
	std::cout << "str1: " << str1 << std::endl << std::endl;

	//用一个字符串的子串重新赋值:string& assign(const string & str, size_t subpos, size_t sublen);
	str2.assign(str1,5,6);
	std::cout << "after -> str2.assign(str1,6,6)" << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	//用C风格的字符串重新赋值:string & assign(const char* s);
	str2.assign("hello C++");
	std::cout << "after -> str2.assign(\"hello C++\")" << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	//用C风格的字符串的前n个重新赋值:string& assign(const char* s, size_t n);
	str2.assign("hello ");
	std::cout << "after -> str2.assign(\"hello\")" << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;

	//用n个相同字符重新赋值:string& assign(size_t n, char c);
	str2.assign(5,'h');
	std::cout << "after -> str2.assign(5,\'h\')" << std::endl;
	std::cout << "str2: " << str2 << std::endl << std::endl;
}

int main()
{
	test_modify_4();

	return 0;
}

运行结果:
str1: hello world
str2: hello string

after -> str1.assign(str2)
str1: hello string

after -> str2.assign(str1,6,6)
str2:  strin

after -> str2.assign("hello C++")
str2: hello C++

after -> str2.assign("hello")
str2: hello

after -> str2.assign(5,'h')
str2: hhhhh

6.插入

函数原型:

  • 在pos位置插入一个字符串:string& insert (size_t pos, const string& str);
  • 在pos位置插入一个字符串的子串:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
  • 在pos位置插入一个C风格的字符串:string& insert (size_t pos, const char* s);
  • 在pos位置插入C风格的字符串的前n个:string& insert (size_t pos, const char* s, size_t n);
  • 在pos位置插入n个相同的字符:string& insert (size_t pos, size_t n, char c);
  • 在迭代器p的位置插入n个相同的字符:void insert (iterator p, size_t n, char c);
  • 在迭代器p的位置插入一个字符:iterator insert (iterator p, char c);

使用示例:

void test_modify_5()
{
	// 在pos位置插入一个字符串:string& insert (size_t pos, const string& str);
	std::string str("hello string");
	std::string t1("C++ ");

	std::cout << "str: " << str << std::endl;
	std::cout << "t1: " << t1 << std::endl;

	str.insert(6,t1);
	std::cout << "after -> str.insert(6,t)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在pos位置插入一个字符串的子串:string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
	std::string t2("hello world ");

	std::cout << "str: " << str << std::endl;
	std::cout << "t2: " << t2 << std::endl;

	str.insert(10, t2, 6, 6);
	std::cout << "after -> str.insert(10, t2, 6, 5)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在pos位置插入一个C风格的字符串:string& insert (size_t pos, const char* s);
	str.assign("hello string");
	std::cout << "str: " << str << std::endl;

	str.insert(6, "C++ ");
	std::cout << "after -> str.insert(6, \"C++\")" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在pos位置插入C风格的字符串的前n个:string& insert (size_t pos, const char* s, size_t n);
	std::cout << "str: " << str << std::endl;
	str.insert(10, "hello world", 6);

	std::cout << "after -> str.insert(10, \"hello world\", 5)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在pos位置插入n个相同的字符:string& insert (size_t pos, size_t n, char c);
	str.assign("hello string");
	std::cout << "str: " << str << std::endl;

	str.insert(6, 5, 'H');
	std::cout << "after -> str.insert(6, 5, \'H\')" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在迭代器p的位置插入n个相同的字符:void insert (iterator p, size_t n, char c);
	str.assign("hello string");
	std::cout << "str: " << str << std::endl;

	std::string::iterator p = str.begin();
	str.insert(p, 5, 'X');

	std::cout << "after -> str.insert(p, 5, \'X\')" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 在迭代器p的位置插入一个字符:iterator insert (iterator p, char c);
	str.assign("hello string");
	std::cout << "str: " << str << std::endl;

	std::string::iterator it = str.begin();
	str.insert(it, 'X');

	std::cout << "after -> str.insert(it, \'X\')" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;
}

int main()
{
	test_modify_5();

	return 0;
}

运行结果:
str: hello string
t1: C++
after -> str.insert(6,t)
str: hello C++ string

str: hello C++ string
t2: hello world
after -> str.insert(10, t2, 6, 5)
str: hello C++ world string

str: hello string
after -> str.insert(6, "C++")
str: hello C++ string

str: hello C++ string
after -> str.insert(10, "hello world", 5)
str: hello C++ hello string

str: hello string
after -> str.insert(6, 5, 'H')
str: hello HHHHHstring

str: hello string
after -> str.insert(p, 5, 'X')
str: XXXXXhello string

str: hello string
after -> str.insert(it, 'X')
str: Xhello string

7.删除

函数原型:

  • 从pos位置开始删除指定长度:string& erase (size_t pos = 0, size_t len = npos);
  • 删除迭代器p位置的字符:iterator erase (iterator p);
  • 删除迭代器区间中的字符:iterator erase (iterator first, iterator last);

使用示例:

void test_modify_6()
{
	// 从pos位置开始删除指定长度:string& erase (size_t pos = 0, size_t len = npos);
	std::string str("hello C++ string");
	std::cout << "str: " << str << std::endl;

	str.erase(6, 3);

	std::cout << "after -> str.erase(6, 3)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 删除迭代器p位置的字符:iterator erase (iterator p);
	str = "hello C string";
	std::cout << "str: " << str << std::endl;

	std::string::iterator it = str.begin() + 6;
	str.erase(it);

	std::cout << "after -> str.erase(it)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;

	// 删除迭代器区间中的字符:iterator erase (iterator first, iterator last);
	str = "hello C++ string";
	std::cout << "str: " << str << std::endl;

	std::string::iterator start = str.begin() + 6;
	std::string::iterator end = str.begin() + 10;

	str.erase(start,end);

	std::cout << "after -> str.erase(start,end)" << std::endl;
	std::cout << "str: " << str << std::endl << std::endl;
}

int main()
{
	test_modify_6();

	return 0;
}

运行结果:
str: hello C++ string
after -> str.erase(6, 3)
str: hello  string

str: hello C string
after -> str.erase(it)
str: hello  string

str: hello C++ string
after -> str.erase(start,end)
str: hello string

8.查找

函数原型:

  • 从指定位置查找一个字符串:size_t find(const string & str, size_t pos = 0) const;

  • 从指定位置查找一个C风格的字符串:size_t find(const char* s, size_t pos = 0) const;

  • 从指定位置查找C风格的字符串的子串:size_t find(const char* s, size_t pos, size_t n) const;

  • 从指定位置查找一个字符:size_t find(char c, size_t pos = 0) const;

    void test_find()
    {
    //从指定位置查找一个字符串:size_t find(const string & str, size_t pos = 0) const;
    std::string str("hello C++ string");
    std::string t("C++");
    std::cout << "str: " << str << std::endl;
    std::cout << "t: " << t << std::endl;

      size_t pos = str.find(t);
      std::cout << "after -> str.find(t)" << std::endl;
      std::cout << "the first pos: " << pos << std::endl << std::endl;
    
      //从指定位置查找一个C风格的字符串:size_t find(const char* s, size_t pos = 0) const;
      str = "hello C++ string";
    
      std::cout << "str: " << str << std::endl;
    
      pos = str.find("C++");
      std::cout << "after -> str.find(\"C++\")" << std::endl;
      std::cout << "the first pos: " << pos << std::endl << std::endl;
    
      //从指定位置查找C风格的字符串的子串:size_t find(const char* s, size_t pos, size_t n) const;
      str = "hello C++ string";
      std::cout << "str: " << str << std::endl;
    
      pos = str.find("string",0,3);
      std::cout << "after -> str.find(\"string\",0,3)" << std::endl;
      std::cout << "the first pos: " << pos << std::endl << std::endl;
    
      //从指定位置查找一个字符:size_t find(char c, size_t pos = 0) const;
      str = "hello C++ string";
      std::cout << "str: " << str << std::endl;
    
      pos = str.find('C',0);
      std::cout << "after -> str.find(\'C\',0)" << std::endl;
      std::cout << "the first pos: " << pos << std::endl << std::endl;
    

    }

    int main()
    {
    test_find();

      return 0;
    

    }

    运行结果:
    str: hello C++ string
    t: C++
    after -> str.find(t)
    the first pos: 6

    str: hello C++ string
    after -> str.find("C++")
    the first pos: 6

    str: hello C++ string
    after -> str.find("string",0,3)
    the first pos: 10

    str: hello C++ string
    after -> str.find('C',0)
    the first pos: 6

9.交换两个字符串

函数原型:

  • void swap (string& str);

使用示例:

void test_modify_7()
{
	std::string str1("hello");
	std::string str2("world");

	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl;

	str1.swap(str2);

	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl;
}

int main()
{
	test_modify_7();

	return 0;
}

运行结果:
str1: hello
str2: world
str1: world
str2: hello

五、字符串的其他操作

1.获取C风格的字符串

函数原型:

  • const char* c_str() const;

使用示例:

void test_1()
{
	std::string str("hello c string");

	auto p = str.c_str();
	std::cout << typeid(p).name();
}

int main()
{
	test_1();

	return 0;
}

运行结果:
char const *

2.复制字符串中的指定内容

函数原型:

  • size_t copy (char* s, size_t len, size_t pos = 0) const;

使用示例:

void test_2()
{
	std::string str("hello string");

	char buffer[20];
	str.copy(buffer, 6, 6);
	buffer[6] = '\0';

	std::cout << buffer;
}

int main()
{
	test_2();

	return 0;
}

运行结果:
string

3.提取子串

函数原型:

  • string substr (size_t pos = 0, size_t len = npos) const;

使用示例:

void test_substr()
{
	// 从指定位置提取指定位置的子串:string substr (size_t pos = 0, size_t len = npos) const;
	std::string str("hello C++ string");
	std::cout << "str: " << str << std::endl;
	
	std::string sub_str = str.substr(6, 3);
	std::cout << "after -> str.substr(6, 3)" << std::endl;
	std::cout << "sub_str: " << sub_str << std::endl;
}

int main()
{
    test_substr();
    return 0;
}

运行结果:
str: hello C++ string
after -> str.substr(6, 3)
sub_str: C++

4.+操作符重载

函数原型:

  • 字符串+字符串:string operator+ (const string& lhs, const string& rhs);
  • 字符串+C风格的字符串:string operator+ (const string& lhs, const char* rhs);
  • C风格的字符串+字符串:string operator+ (const char* lhs, const string& rhs);
  • 字符串+字符:string operator+ (const string& lhs, char rhs);
  • 字符+字符串:string operator+ (char lhs, const string& rhs);

使用示例:

void test_operator_add()
{
	// 字符串+字符串:string operator+ (const string& lhs, const string& rhs);
	std::string str1("hello");
	std::string str2(" string");
	std::cout << "str1: " << str1 << std::endl;
	std::cout << "str2: " << str2 << std::endl;

	std::string str3 = str1 + str2;
	std::cout << "after -> str3 = str1 + str2" << std::endl;
	std::cout << "str3: " << str3 << std::endl << std::endl;

	// 字符串+C风格的字符串:string operator+ (const string& lhs, const char*   rhs)
	std::string str4("hello");

	std::cout << "str4: " << str4 << std::endl;

	std::string str5 = str4 + " string";
	std::cout << "after -> str5 = str4 +\"string\"" << std::endl;
	std::cout << "str5: " << str5 << std::endl << std::endl;

	// C风格的字符串+字符串:string operator+ (const char*   lhs, const string& rhs);
	std::string str6("string");

	std::cout << "str6: " << str6 << std::endl;

	std::string str7 = "hello " + str6;
	std::cout << "after -> str7 = \"hello \" + str6" << std::endl;
	std::cout << "str7: " << str7 << std::endl << std::endl;

	// 字符串+字符:string operator+ (const string& lhs, char rhs);
	std::string str8("hello ");

	std::cout << "str8: " << str8 << std::endl;

	std::string str9 = str8 + 'C';
	std::cout << "after -> str9 = str8 + \'C\'" << std::endl;
	std::cout << "str9: " << str9 << std::endl << std::endl;

	// 字符+字符串:string operator+ (char lhs, const string& rhs);
	std::string str10(" string");

	std::cout << "str10: " << str10 << std::endl;

	std::string str11 = 'C'+str10;
	std::cout << "after -> str11 = \'C\'+str10" << std::endl;
	std::cout << "str11: " << str11 << std::endl << std::endl;
}

int main()
{
    test_operator_add();
    return 0;
}

运行结果:
str1: hello
str2:  string
after -> str3 = str1 + str2
str3: hello string

str4: hello
after -> str5 = str4 +"string"
str5: hello string

str6: string
after -> str7 = "hello " + str6
str7: hello string

str8: hello
after -> str9 = str8 + 'C'
str9: hello C

str10:  string
after -> str11 = 'C'+str10
str11: C string

5.获取一行

函数原型:

  • istream& getline(istream & is, string & str);

使用示例:

void test_getline()
{
	// 获取一行:istream& getline(istream & is, string & str);
	std::string str;
	std::getline(std::cin,str); // 输入:hello C++ string

	std::cout << "str: " << str << std::endl;
}

int main()
{
	test_getline();

	return 0;
}

运行结果:
hello C++ string
str: hello C++ string
相关推荐
Fan_5583 分钟前
008 Qt_显示类控件_QLabel
开发语言·qt
No0d1es8 分钟前
GESP CCF C++六级编程等级考试认证真题 2024年12月
开发语言·c++·算法·青少年编程·gesp·ccf·六级
IT199511 分钟前
Qt笔记-Qt Creator开发环境搭建
开发语言·笔记·qt
编码小哥16 分钟前
C++信号处理
c++
爱上语文19 分钟前
宠物管理系统:Service层
java·开发语言·宠物
机器视觉知识推荐、就业指导22 分钟前
C++设计模式:组合模式(公司架构案例)
c++·后端·设计模式·组合模式
意疏29 分钟前
【C 语言指针篇】指针的灵动舞步与内存的神秘疆域:于 C 编程世界中领略指针艺术的奇幻华章
c语言·开发语言·指针
水w29 分钟前
【项目实践】SpringBoot Nacos配置管理 map数据
java·服务器·开发语言·spring boot·nacos
长安051130 分钟前
面试经典题目:LeetCode134_加油站
c++·算法·面试
喵手34 分钟前
Java 实现日志文件大小限制及管理——以 Python Logging 为启示
java·开发语言·python