STL string

/*
	STL string c++字符串
*/
#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
#include <tchar.h>

int main()
{
	char name[20];	// C语言 的字符串
	char *name1 = "dskfdsf";	// // C语言 的字符串
	
	// c++字符串
	//using namespace std;
	//std::string

	string str("你是");
	string str1 = "dfsdf";

	const char* lala = "dsfsdf!";
	std::string ss(lala);

	std::string strPartialCopy(lala, 5);

	std::string strRepeatChars(10, 'a');

	const char *pszConstStr = "Hello C";
	char *pszCopy = new char(strlen(pszConstStr) + 1);
	strcpy(pszCopy, pszConstStr);
	cout<<pszCopy<<endl;
	//delete[] pszCopy;




	return 0;
}

<span style="color:#ff0000;">字符串连接</span>

<pre name="code" class="cpp">#include <iostream>
//
#include <string>

using namespace std;

int main()
{
	cout << "请输入一行字符串!" << endl;

	string strSample1("Hello");
	string strSample2("String!");

	strSample1 += strSample2;

	string strSample3("Fun is not needing to use pointers");
	strSample1.append(strSample3);

	const char *pszConstString = "You however still can!";	// c语言风格的字符串
	strSample1.append(pszConstString);


	system("pause");
	return 0;
}


 

字符串反转

#include <iostream>
//
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	string strSample("Hello String! We will reverse you!");
	cout << strSample << endl;

	reverse(strSample.begin(), strSample.end());
	cout << strSample << endl;



	system("pause");
	return 0;
}
截取

#include <iostream>
//
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string strSample("Hello String! Wake up to a beautiful day!");
	cout << strSample << endl;
	// erase
	strSample.erase(13, 28);
	cout << strSample << endl;

	// find 算法,迭代器
	string::iterator iCharS = find(strSample.begin(), strSample.end(), 'S');
	if(iCharS != strSample.end())
	{
		strSample.erase(iCharS); 
	}

	cout << strSample << endl;

	strSample.erase(strSample.begin(), strSample.end());

	if(strSample.length() == 0)
	{
		cout << "The string is empty!" << endl;
	}
	system("pause");
	return 0;
}

迭代

#include <iostream>
#include <string>


//
using namespace std;
int main()
{
	string strSTLString("Hello String");
	
	for(size_t i = 0; i < strSTLString.length();++i)
	{
		cout<<strSTLString[i] << endl;
	}


	//
	cout << "使用STL里面的迭代器!" << endl;
	string::const_iterator itr;
	for(itr = strSTLString.begin(); itr != strSTLString.end(); ++itr)
	{
		cout << *itr << endl;	// 迭代器是一个指针
	}


	system("pause");
	return 0;
}

查找

#include <iostream>
#include <string>
//
using namespace std;
int main()
{
	string strSample("Good day String! Today is beautiful!");
	cout << strSample << endl;


	size_t nOffset =  strSample.find("day", 0);	// 从0 开始找
	if(nOffset != string::npos)	// npos  实际上是-1
	{
		cout << "在下标:" << nOffset << "找到了!" << endl;
	}
	else
	{
		cout << "没有找到!" << endl;
	}


	// 查找所有的day
	cout << "\n\n查找所有的day:"<< endl;


	size_t nSubstringOffset = strSample.find("day", 0);
	while(nSubstringOffset != string::npos)
	{
		cout << "在下标这里:" << nSubstringOffset << "找到day!"<< endl;
		size_t nSearchOffset = nSubstringOffset + 1;
		nSubstringOffset = strSample.find("day", nSearchOffset);
	}


	//
	cout << "\n\n查找所有的字符a:" << endl;
	size_t nCharacterOffset = strSample.find('a', 0);
	while(nCharacterOffset != string::npos)
	{
		cout << "在下标这个:" << nCharacterOffset << "找到a!" << endl;
		size_t nCharSearchOffset = nCharacterOffset + 1;
		nCharacterOffset = strSample.find('a', nCharSearchOffset);
	}






	system("pause");


	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值