c++字符串类型string和char数组的互转

1. string转char*

#include
#include

int main()
{
	std::string boygirl_name_string = "hablee and yuki";
	char boygirl_name_chars[256];

	sprintf_s(boygirl_name_chars, boygirl_name_string.c_str()); // string -> char*
	std::cout << boygirl_name_chars << std::endl;

	return 0;
}

c++字符串类型string和char数组的互转_第1张图片

2. char* 转 string

#include
#include
#include

int main()
{
	std::string boygirl_name_string;
	char boygirl_name_chars[256] = "hablee and yuki";

	std::stringstream ss;
	ss << boygirl_name_chars;
	boygirl_name_string = ss.str();
	ss.str(""); // 用完清空

	std::cout << boygirl_name_string << std::endl;

	return 0;
}

c++字符串类型string和char数组的互转_第2张图片

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