[C++] STL container - string string operation ③ (string string and char* string conversion | string#c_str() member function | string#copy() member function)






1. String string and char* string conversion



1. String and char* conversion


The string string class encapsulates the char* character pointer;

  • Converting a string string to a char* string means taking out the encapsulated char* character pointer;
  • Converting char* string to string string means creating a string string based on char* string;

2. Convert string to char* - c_str() member function


In the std::string class in the C++ language, a c_str() member function is encapsulated, which is used to return a constant character pointer pointing to the content of the string;

To convert string to char* type, you need to call c_str() member function;

c_str()The prototype of the function is as follows:

const char* c_str() const;

c_str() function returns a constant character pointer pointing to the content of the string. This pointer can be used to interact with C language library functions, such as using the printf() function to print a string;


Code example:

	string s1 = "123456789";

	// 将 string 转为 char* 
	const char* s2 = s1.c_str();

	cout << "s2 : " << s2 << endl;

3. Convert string to char* - copy() member function


std::string class copy() member function, the prototype is as follows:

void copy(char* dest, size_t len, size_t pos = 0);

The function of this function is to copy the characters starting from the pos position in the string to the target character array Medium; By default, the parameter is , which means copying from the beginning of the string;lendestpos0


Code example:

	string s1 = "123456789";

	// 为 字符指针 分配内存
	// 分配完内存后 使用 0 初始化
	// 防止随机内容出现乱码导致字符串没有 '\0' 结尾
	char buf[16] = {
    
    0};

	// 将 s1 字符串 "123456789" 的 
	// 从 0 开始的 3 个字符 拷贝到 buf 中
	s1.copy(buf, 3, 0);

3. Convert char* to string


To convert char* to a string string, you only need to pass the char* string into the character array constructor of the string object;

The character array constructor of the string class accepts a pointer s to a character array and creates the contents of the array as a string;

std::string(const char* s);

Code example:

	// 将 char* 转为 string
	string s3(s2);

4. Code example - conversion between char* and string


Code example:

#include "iostream"
using namespace std;
#include "string"

int main() {
    
    

	string s1 = "123456789";

	// 将 string 转为 char* 
	const char* s2 = s1.c_str();

	cout << "s2 : " << s2 << endl;

	// 将 char* 转为 string
	string s3(s2);

	cout << "s3 : " << s3 << endl;

	// 为 字符指针 分配内存
	// 分配完内存后 使用 0 初始化
	// 防止随机内容出现乱码导致字符串没有 '\0' 结尾
	char buf[16] = {
    
    0};

	// 将 s1 字符串 "123456789" 的 
	// 从 0 开始的 3 个字符 拷贝到 buf 中
	s1.copy(buf, 3, 0);

	cout << "buf : " << buf << endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

s2 : 123456789
s3 : 123456789
buf : 123
Please press any key to continue. . .< /span>

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135036916