C/C++ string,char * p

本文探讨了C/C++中的字符串初始化,详细阐述了两种字符串(string和char*)之间的相互转化,并且深入解析了data()和c_str()方法的区别。在实践中,通过调试和源码分析发现data()实际上调用了c_str(),文章还提及了相关的类型转换话题,但内容尚未完整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C/C++字符串的初始化
    char * cstr = (char *)malloc(100);
	scanf("%s", cstr);            //在输入之前一定要先分配内存
	printf("%s\n", cstr);
	char cbuf[10];
	scanf("%s", &cbuf);
	scanf("%s", cbuf);			//这两种方式都可以
	printf("%s\n", cbuf);

	strcat(cstr, cbuf);			//c字符串的拼接


	string str;//这个是C++提供的字符类,并不是基本数据类型

	string s1(cbuf);
	string s2(cstr);//构造函数可以是c中的字符数组

	string s3(4, '=');//"===="

	string s4(s3);
	string s5 = s4;

	string s6 = s5 + s4;		//字符串的拼接
	//string类型不能用printf进行输出,只能用cout
	//printf("%s\n",s6);//只支持c语言的字符串
两种字符串的相互转化
	const char * data = s6.data();
	const char * c_str = s6.c_str();
	cout << "data()?c_str()" << (s6.data() ==s6.c_str()) << endl;  //值为1
	string s7=cstr;
	string s8=cbuf;
	
data()和c_str()区别?

在这里插入图片描述
看调试过程,源码,data()调用的c_str()没有什么区别。

//"abc"存储在静态变量区,str1和str2根据"abc"的大小分别开辟了4个字节的空间,所以他们的值并不相等
	//并把"abc\0"赋值赋给开辟的空间。
	char str1[] = "abc";
	char str2[] = "abc";
	//"abc"已经在静态存储区了,所以并不会在开辟空间存储"abc"了。
	//如果后面不对str3和str4进行比较,编译器就不会为str3和str4分配内存,只存储在符号表中
	//但是后面对他们进行了比较,编译器就会分配内存,分配了不同的内存,地址肯定不一样,所以结果为false
	const char str3[] = "abc";
	const char str4[] = "abc";
	//"abc"已经在静态存储去了,所以str5和str6直接指向"abc"的内存就可以了,所以他们的值相等
	const char* str5 = "abc";
	const char* str6 = "abc";
	cout << (str1 == str2) << endl;//false  比较他们的值,地址肯定不一样
	cout << (str3 == str4) << endl;//false  也是比较他们的值,地址不一样
	cout << (str5 == str6) << endl;//true   两个指针指向同一位置,地中相同


	string ss1 = "12345";
	string ss2 = "12345";

	cout << (&ss1 == &ss2) << endl;//false  ss1 和ss2是两个不同的对象
	cout << (ss1 == ss2) << endl;  //true   两个值相等
类型转换

在这里插入图片描述

//整数类型转为字符串类型
	double f = 23.43;
	double f2 = 1e-9;
	double f3 = 1e40;
	double f4 = 1e-40;
	double f5 = 123456789;
	string f_str = to_string(f);
	string f_str2 = to_string(f2); // 注意:返回 "0.000000"
	string f_str3 = to_string(f3); // 注意:不返回 "1e+40".
	string f_str4 = to_string(f4); // 注意:返回 "0.000000"
	string f_str5 = to_string(f5);
	cout << "std::cout: " << f << '\n'
		<< "to_string: " << f_str << "\n\n"
		<< "std::cout: " << f2 << '\n'
		<< "to_string: " << f_str2 << "\n\n"
		<< "std::cout: " << f3 << '\n'
		<< "to_string: " << f_str3 << "\n\n"
		<< "std::cout: " << f4 << '\n'
		<< "to_string: " << f_str4 << "\n\n"
		<< "std::cout: " << f5 << '\n'
		<< "to_string: " << f_str5 << '\n';
	/*  输出
		std::cout: 23.43
		to_string : 23.430000

		std::cout : 1e-09
		to_string : 0.000000

		std::cout : 1e+40
		to_string : 10000000000000000303786028427003666890752.000000

		std::cout : 1e-40
		to_string : 0.000000

		std::cout : 1.23457e+08
		to_string : 123456789.000000
	*/
	//将字符串类型转为整型
	std::string str1 = "45";
	std::string str2 = "3.14159";
	std::string str3 = "31337 with words";
	std::string str4 = "words and 2";

	int myint1 = std::stoi(str1);
	int myint2 = std::stoi(str2);
	int myint3 = std::stoi(str3);
	// 错误: 'std::invalid_argument'
	// int myint4 = std::stoi(str4);

	std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
	std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
	std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
	//输出如下
	//std::stoi("45") is 45
    //std::stoi("3.14159") is 3
	//std::stoi("31337 with words") is 31337

在这里插入图片描述

	const char *str1 = "42";
    const char *str2 = "3.14159";
    const char *str3 = "31337 with words";
    const char *str4 = "words and 2";
 
    int num1 = std::atoi(str1);
    int num2 = std::atoi(str2);
    int num3 = std::atoi(str3);
    int num4 = std::atoi(str4);
 
    std::cout << "std::atoi(\"" << str1 << "\") is " << num1 << '\n';
    std::cout << "std::atoi(\"" << str2 << "\") is " << num2 << '\n';
    std::cout << "std::atoi(\"" << str3 << "\") is " << num3 << '\n';
    std::cout << "std::atoi(\"" << str4 << "\") is " << num4 << '\n';
    //std::atoi("42") is 42
    //std::atoi("3.14159") is 3
    //std::atoi("31337 with words") is 31337
    //std::atoi("words and 2") is 0

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值