《c++越野》数据篇-string

发布时间 2023-03-30 10:43:48作者: Fusio

string.h和string的区别

<string>:是包装了std 的C++头文件,对应的是新的string 类,string s1就是建立一个string类的对象
<string.h>:是旧的C 头文件,对应的是基于char*的字符串处理函数,的c语言的东西 并无类,所以不能 string s1 。
<cstring>:是对应于旧C 头文件的std 版本。(包含std),<cstring>文件实际上只是在一个命名空间std中include了 <string.h>

在什么时候需要“#include string.h“

原文链接:https://www.cnblogs.com/FKdelphi/p/6026851.html

C++中,string头文件基本上已经包含在iostream中了。
但是,平时使用的时候建议加上#include<string.h>(尤其在以下情况下)

  • 1、使用string类型
  • 2、使用cin、cout语句来输入输出string类型变量(注意,同时还需要#include
  • 3、使用memset()、strlen()、strcpy()等函数时。

C++中的string类用法简介

原文链接:https://blog.csdn.net/liitdar/article/details/80498634

概述

string是C++标准库的一个重要的部分,主要用于字符串处理。

c_str(),string转换为char*

// 方法一:使用 c_str() 方法,代码(stringsimple.cpp)如下:

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
	string strOutput = "Hello World";
	cout << "[cout] strOutput is: " << strOutput << endl;
	// string 转换为 char*
	const char* pszOutput = strOutput.c_str();
	printf("[printf] strOutput is: %s\n", pszOutput);
	return 0;
}

结果:
image

上述代码执行结果说明:

  • cout 可直接输出 string 类的对象的内容;
  • 使用 c_str() 方法转换 string 类型到 char* 类型时,需要为char*添加 const 关键字;
  • printf() 函数不能直接打印 string 类的对象的内容,可以通过将 string 转换为 char* 类型,再使用 printf() 函数打印。

data()方法与c_str()方法

data()方法与c_str()方法相似,都返回 const char* 类型。两者区别和联系如下:

  • 在C++98版本中,c_str()返回 const char* 类型,返回的字符串会以空字符(null character)结尾;
  • 在C++98版本中,data()返回 const char* 类型,返回的字符串不以空字符(null character)结尾;
  • 在C++11版本中,c_str()与data()用法相同(Both string::data and string::c_str are synonyms and return the same value.)

string.length()计算长度、string.compare()字符串比较

示例代码如下:

#include <string>
#include <iostream>

#define HELLOSTR "Hello World"

using namespace std;

int main()
{
	string strOutput = "Hello World";
	int nLen = strOutput.length();
	cout << "the length of strOutput is: " << nLen << endl;
	if (0 == strOutput.compare(HELLOSTR))
	{
		cout << "strOutput equal with macro HELLOSTR" << endl;
	}
	return 0;
}

结果如下:

[root@node1 /opt/liitdar/mydemos/simples]# ./stringsimple2 
the length of strOutput is: 11
strOutput equal with macro HELLOSTR
[root@node1 /opt/liitdar/mydemos/simples]#

上述代码执行结果说明:

  • string类型可直接使用 length() 方法计算字符串长度,该方法计算结果为字符串的实际长度,如本例中"Hello World"字符串的长度为11;
  • string类型可使用 compare(const string& str) 方法进行字符串比较。

string.empty()对象判空

可使用 empty() 方法对string类型的对象进行判空,如下:

if (str2.empty())
{
	cout << "str2 is empty." << endl;
}

char*、char[]转换为string

将 char* 、char[] 转换为 string 类型时,直接进行赋值操作,将 char*、char[] 的变量赋值给 string 对象即可。

说明:这里所说的“赋值”操作,实际上是将 char*、char[] 定义的字符串的首地址赋值给 string 对象了。

示例代码(stringtochar.cpp)如下:

#include <string>
#include <iostream>

using namespace std;

int main()
{
	const char* pszName = "liitdar";
	char pszCamp[] = "alliance";

	string strName;
	string strCamp;

	strName = pszName;
	strCamp = pszCamp;

	cout << "strName is: " << strName << endl;
	cout << "strCamp is: " << strCamp << endl;

	return 0;
}

image

string.find方法,字符串是否存在

使用string类的find方法,在字符串中检索自字符串是否存在。

#include <string>
#include <iostream>

using namespace std;

int main()
{
	// 待检索的字符串
	string strOutput = "|0|1|2|";
	// 需要检索的子串
	string strObj = "|1|";

	// 子串位于字符串中的位置
	size_t nLoc = strOutput.find(strObj);
	// 如果检索到子串在字符串中,则打印子串的位置
	if (nLoc != string::npos)
	{
		cout << "nLoc is: " << nLoc << endl;
	}

	return 0;
}

image

string.insert方法,向字符串中插入字符(串)

使用string类的insert方法,向字符串中插入字符(串)。

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string strDemo = "I am";

	strDemo.insert(4, " good.");

	cout << "strDemo is: " << strDemo << endl;

	return 0;
}

image

int类型转为string类的方法

这里介绍两种常见的 int 类型转换为 string 类的方法,示例代码如下:

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
	// 方法1
	int nNum1 = 123;
	stringstream ss;

	ss << nNum1;
	string strTest1 = ss.str();
	cout << "strTest1 is: " << strTest1 << endl;

	/*
	string strTest2;
	strTest2 << ss;     // stringstream 未定义 << 操作符,故此句报错
	cout << "strTest2 is: " << strTest2 << endl;
	*/

	string strTest3;
	ss >> strTest3;
	cout << "strTest3 is: " << strTest3 << endl;

	// 方法2
	int nNum2 = 456;
	string strTest4;
	strTest4 = to_string(nNum2);    // C++11 标准
	cout << "strTest4 is: " << strTest4 << endl;

	return 0;
}

image