STL sting比c语言字符串好:因为c语言不是字符串,只是一个字符指针或者字符数组。
STL string 的常用操作主要有以下五种:复制,迭代,连接,查找,截短;
STL string 的常用算法有:字符串反转,大小写转换;
复制
#include <iostream>
#include <string>
//
int main()
{
// c语言中字符串 ,并不是真正意义上的字符串
char name[20] = "你好!";
char *name2 = "你好!";
// c++中的字符串
using namespace std;
string str("你好!");
cout << name[20] + "dsfdsfsdfdsfds" << endl;
cout << *name2 << endl;
cout << str << endl;
string str2 = "我好!"; // 复制
cout << str2 << endl;
const char *constString = "Hello World!";// c语言风格的
std::string strFromConst(constString); // c++风格的 // 复制
cout << strFromConst << endl;
std::string str3("Hello String!");
std::string str3Copy(str3); // 复制
cout << str3Copy << endl;
std::string strPartialCopy(constString, 4); // 只复制前4个
cout << strPartialCopy << endl;
std::string strRepeatChars(10, 'a'); // 初始化 10个a
cout << strRepeatChars << endl;
// c语言中的复制
const char *pszConstStr = "Hello C World!";
cout << "复制前:";
cout <<pszConstStr << endl;
char *pszCopy = new char(strlen(pszConstStr) + 1);
strcpy(pszCopy, pszConstStr);
cout << "复制后:";
cout << pszCopy << endl;
//delete[] pszCopy;
system("pause");
return 0;
}
运行程序会报 错误
1
error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details
原因是Visual C++ 2012 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些带有“_s”后缀的函数),请参见:
《CRT函数的安全增强的版本》
http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html
下面给出这个问题的解决方案:
方法一:将原来的旧函数替换成新的 Security CRT functions。
方法二:用以下方法屏蔽这个警告:
1. 在预编译头文件stdafx.h里(注意:一定要在没有include任何头文件之前)定义下面的宏:
#define _CRT_SECURE_NO_DEPRECATE
2. 或声明 #param warning(disable:4996)
3. 更改预处理定义:
项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加:
_CRT_SECURE_NO_DEPRECATE
方法三:方法二没有使用更加安全的 CRT 函数,显然不是一个值得推荐的好方法,但我们又不想一个一个地改函数名,这里还有一个更简便的方法:
在预编译头文件 stdafx.h 里(同样要在没有include任何头文件之前)定义下面的宏:
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
在链接的时候便会自动将旧函数替换成 Security CRT functions 。
注意:这个方法虽然使用了新的函数,但是不能消除警告(原因见红字),你还得同时使用方法二(-_-)。即实际应在预编译头文件 stdafx.h 里加入下面两句:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
错误原因解释:
这种微软的警告,主要因为那些C库的函数,很多函数内部是不进行参数检测的(包括越界类的),微软担心使用这些会造成内存异常,所以就改写了同样功能的函数,改写了的函数进行了参数的检测,使用这些新的函数会更安全和便捷。关于这些改写的函数你不用专门去记忆,因为编译器对于每个函数在给出警告时,都会告诉你相应的安全函数,查看警告信息就可以获知,在使用时也再查看一下MSDN详细了解。
参考资料:《安全模板重载》
用上面的第一种方法成功解决问题如下:#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
//
int main()
{
// c语言中字符串 ,并不是真正意义上的字符串
char name[20] = "你好!";
char *name2 = "你好!";
// c++中的字符串
using namespace std;
string str("你好!");
cout << name[20] + "dsfdsfsdfdsfds" << endl;
cout << *name2 << endl;
cout << str << endl;
string str2 = "我好!"; // 复制
cout << str2 << endl;
const char *constString = "Hello World!";// c语言风格的
std::string strFromConst(constString); // c++风格的 // 复制
cout << strFromConst << endl;
std::string str3("Hello String!");
std::string str3Copy(str3); // 复制
cout << str3Copy << endl;
std::string strPartialCopy(constString, 4); // 只复制前4个
cout << strPartialCopy << endl;
std::string strRepeatChars(10, 'a'); // 初始化 10个a
cout << strRepeatChars << endl;
// c语言中的复制
const char *pszConstStr = "Hello C World!";
cout << "复制前:";
cout <<pszConstStr << endl;
char *pszCopy = new char(strlen(pszConstStr) + 1);
strcpy(pszCopy, pszConstStr);
/*strcpy_s(pszCopy, pszConstStr);*/
//strcpy_s(pszCopy, pszConstStr);
cout << "复制后:";
cout << pszCopy << endl;
//delete[] pszCopy;
system("pause");
return 0;
}
总结:复制
1,声明时的复制。
std::string strFromConst(constString); // c++风格的 // 复制
std::string strRepeatChars(10, 'a'); // 初始化 10个a
2,复制某一字符串前几个。
const char *constString = "Hello World!";// c语言风格的
<pre code_snippet_id="1772061" snippet_file_name="blog_20160719_2_8482431" name="code" class="cpp">std::string strPartialCopy(constString, 4); // 只复制前4个
迭代
/*
字符串迭代
*/
#include <iostream>
#include <string>
//
int main()
{
using namespace std;
string strSTLString("Hello String!");
cout << "使用传统的方法显示字符串中的每一个字符:"<< endl;
for (size_t nCharCounter = 0;
nCharCounter < strSTLString.length();
++nCharCounter)
{
cout << strSTLString[nCharCounter] << endl;
}
//
cout << "使用STL里面的迭代器操作字符串中的每一个字符:" << endl;
string::const_iterator itr; //迭代器是一个指针
for (itr = strSTLString.begin(); itr != strSTLString.end(); ++itr)
{
cout << *itr << endl;
}
cout << strSTLString.c_str() << endl; // 变为c风格的字符串
//
system("pause");
return 0;
}
总结:
1,申明一个迭代器
string::const_iterator itr;
2,字符串初始位置
itr = strSTLString.begin()
3,字符串结尾位置
itr != strSTLString.end()
连接
/*
连接
*/
#include<iostream>
#include<string>
int main()
{
using namespace std;
string strSample1("Hello");
string strSample2(" String!");
strSample1 += strSample2;
cout << strSample1 << endl;
string strSample3("Fun is not needing to use pointers!");
strSample1.append(strSample3);
cout << strSample1 << endl;
const char *pszConstString = "You howver still can!"; // c风格的字符串
strSample1.append(pszConstString);
cout << strSample1 << endl;
system("pause");
return 0;
}
总结:连接
一个是 + ; 一个是 append();
查找
/*
查找
*/
#include<iostream>
#include<string>
int main()
{
using namespace std;
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<<"找到了day!"<< endl;
}
else{
cout << "没找到!" << endl;
}
// 查找字符串
cout << "\n查找所有的day!" << endl;
size_t nSubstringOffset = strSample.find("day", 0);
while (nSubstringOffset != string::npos)
{
cout << "在下标:" << nSubstringOffset << "找到了day!" << endl;
nSubstringOffset = strSample.find("day", nSubstringOffset + 1);
}
// 查找所有的字符
cout << "\n\n查找所有的o:" << endl;
size_t nCharacterOffset = strSample.find('o', 0);
while (nCharacterOffset != string::npos)
{
cout << "在下标:" << nCharacterOffset << "找到了o!" << endl;
nCharacterOffset = strSample.find('o', nCharacterOffset + 1);
}
system("pause");
return 0;
}
总结:查找
1, find
size_t nOffset = strSample.find("day", 0); // 从0的位置开始找
string::npos 为 -1的位置
截短
/*
截短
*/
#include<iostream>
#include<string>
//
#include <algorithm>
int main()
{
using namespace std;
string strSample("Good day String! Today iS beautiful!");
cout << strSample << endl;
strSample.erase(5, 8);// 删除字符串strSample从5位置起后8个字符
cout << strSample << endl;
//find 算法 迭代器
string::iterator iCharS = find(strSample.begin(), strSample.end(), 'S'); // 要用find 必须加入算法头文件
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;
}
总结:
删除函数 erase()
字符串反转 大小写转换
/*
字符串反转
*/
#include<iostream>
#include<string>
//
#include <algorithm>
int main()
{
using namespace std;
//
cout << "输入一行字符串:" << endl;
string strInput;
getline(cin, strInput);
// transfom 字符串转换 从开头到结尾,全部为大写,并把结果放到begin的位置(第三个参数)上
transform(strInput.begin(), strInput.end(), strInput.begin(), toupper);
cout << strInput << endl;
transform(strInput.begin(), strInput.end(), strInput.begin(), tolower);
cout << strInput << endl;
//
system("pause");
return 0;
}
总结:
字符串反转 reverse
字符串大小写转换
// transfom 字符串转换 从开头到结尾,全部为大写,并把结果放到begin的位置(第三个参数)上
transform(strInput.begin(), strInput.end(), strInput.begin(), toupper);