版权声明:所有版权归作者她的吻让他,转载请标明出处. https://blog.csdn.net/qq_37059136/article/details/84307125
#include <Windows.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
#if 0
/*********************************************/
//string 的各种赋值
string str_1 = "string_1";
//string str_2(str_1); //拷贝构造string串
//string str_2(str_1,3); //拷贝从第三个位置(包括)向后所有字符
string str_2(str_1,3,3); //拷贝从第三个位置(包括)向后所有3个字符
cout << "str_2 = " << str_2 << endl;
char * pcstr = "Character string 1";
//string str_3(pcstr); //拷贝构造char字符串
string str_3(pcstr,4); //拷贝char字符串前4个字符
cout << "str_3 = " << str_3 << endl;
str_3.~string();
if (!str_3.empty())
{
cout << "str_3 = " << str_3 << endl;
}
/*********************************************/
//string 中的swap交换函数
str_2.swap(str_1); //交换str_1与str_2的值
cout << str_2 << endl;
cout << str_1 << endl;
/*********************************************/
//string串赋新值
string str_4(str_2);
cout << "str_4 = " << str_4 << endl; //输出str_4 = string_1
str_4 = "string_4"; //将str_4赋予新值,str_4会抛弃原值,获得新值 输出 str_4 = string_4
cout << "str_4 = " << str_4 << endl;
str_4.assign(str_1);
cout << str_4 << endl; //str_4 重新赋值,输出ing
/****************************************************/
//string的删除
str_4.erase(3); //虽然是删除,但其实是按erase()括号内数字输出相应位数字符串 例如这里输出ing
cout << str_4 << endl;
str_4.erase(2); //输出in
cout << str_4 << endl;
str_4.erase(1); //输出i
cout << str_4 << endl;
str_4.erase(); //输出为空
cout << str_4 << endl;
//这里解释为erase()函数为全部删除,括号内数字为删除时保留的内容长度s
string str_5 = "string_5";
cout << str_5 << endl;
str_5.clear(); //清空str_5字符串,功能类似erase()
cout << str_5 << endl;
/****************************************************/
//string插入字符
string str_6("string_6");
str_6.insert(8,"add"); //在指定位置插入字符串,输出string_6add
cout << str_6 << endl;
/**********************************************/
//string替换字符
string str_7("string_7");
str_7.replace(0,3,"STR"); //从0开始将后面三个字符替换,输出STRing_7
cout << str_7 << endl;
/**********************************************/
//string字符串比较
string str_8("string_8");
string str_8_("string_8");
if (str_8_ == str_8)
{
cout << "相同" << endl;
}
string str_8_1("1");
string str_8_2("2");
if (str_8_1 > str_8_2)
{
cout << str_8_1 << endl;
}
else
{
cout << str_8_2 << endl; //输出2
}
if (str_8.compare(str_8_) == 0)
{
cout << "str_8_和str_8相同" << endl;
}
//string支持大小比较,(不)等于比较
/**************************************************/
//string字符数目
string str_9("string_9");
int istrSize = str_9.size();
int istrLength = str_9.length();
if (istrLength == istrSize)
{
cout << "istrSize==istrSize = " << istrSize << endl; //输出istrSize==istrSize = 8
}
//基本上size跟length值相同,目前不清楚他们取值不同的情况
DWORD istrMax_size = str_9.max_size();
cout << istrMax_size << endl; //输出string串所能使用的最大长度,int装不下,使用DWORD,输出4294967294
/**************************************************/
//string判断是否为空
string str_10;
if (str_10.empty())
{
cout << "str_10为空" << endl; //输出str_10为空
}
//当string对象为空时,empty()返回真,否则返回假
/**************************************************/
//string字符容量
string str_11("s");
cout << str_11.capacity() << endl; //初始string分配15个字符内存
str_11+="01234567890123";
cout << "str_11.length = " << str_11.length() << endl; //输出15
cout << "str_11.capacity = " << str_11.capacity() << endl; //输出15
str_11+="4";
cout << "str_11.length = " << str_11.length() << endl; //输出16
cout << "str_11.capacity = " << str_11.capacity() << endl; //输出31
//string 初始分配15个字符大小,当字符数不大于15时,capacity始终是15
//当字符数大于15,capacity扩大
//每次超出当前容量,capacity扩大,string内存分配按照:(n*16-1)分配
/**************************************************/
//string分配内存大小
string str_12("string_12");
cout << str_12.capacity() << endl; //输出15
str_12.reserve(20);
cout << str_12.capacity() << endl; //输出31
//虽然想分配20个字符空间但由于string空间只能是(n*16-1),所以实际大小为31而不是20
/**************************************************/
//string存取单一字符
string str_13("string_13");
cout << str_13[0] << endl; //string字符串可以看做char数组,用下标操作 输出s
cout << str_13.at(1) << endl; //输出t
//使用位置操作string时需注意最大位置不能越界
#endif
/**************************************************/
#if 0
//string从stream中读取某值
string str_14;
int i = 0;
while (getline(cin,str_14)) //输入字符串,回车结束单次输入,Crtl+Z退出循环 讲解网址http://www.cnblogs.com/ymd12103410/p/9514896.html
{
cout << str_14 << endl;
i++;
if (i == 1) //可以使用计数来控制循环退出
{
break;
}
}
#endif
#if 0
string str_14_1;
string str_14_2;
stringstream sstr_14; //字串型的串流(stream),要使用 stringstream必須先加入這一行:#include <sstream>
getline(cin,str_14_1);
sstr_14.clear();
sstr_14.str(str_14_1);
sstr_14 >> str_14_2;
cout << str_14_2 << endl;
#endif
#if 0
//从键盘输入一个数,表示接下来有几行输入,每行输入不超过200字符,将每行输入的数字求和
int n,a,sum;
string strSaveCin;
stringstream ss; //字串型的串流(stream)
cout << "请输入一个整数" << endl;
cin >> n;
getline(cin,strSaveCin); //读取换行
for (int i = 0;i < n;i++)
{
getline(cin,strSaveCin);
ss.clear();
//ss.str(strSaveCin);
ss << strSaveCin ; //这一句跟上面注释掉的相同
sum = 0;
while (1)
{
ss >> a;
if (ss.fail())
{
break;
}
sum+=a; //这句必须放在判断下面,不然会多加一次最后的数字
}
cout << sum << endl;
}
#endif
/**************************************************/
//string将某值复制为一个char *
string str_15 = "string_15";
char * pstr_15 = new char[20];
if (pstr_15)
{
ZeroMemory(pstr_15,20);
}
str_15.copy(pstr_15,6,0); //输出string
cout << pstr_15 << endl;
if (pstr_15)
{
delete pstr_15;
}
/**************************************************/
//string转char *
string str_16("string_16");
const char * pstr_16 = str_16.c_str(); //输出string_16
cout << pstr_16 << endl;
/**************************************************/
//string 转字符数组
string str_17("string_17");
const char * pstr_17 = str_17.data(); //data跟c_str的区别就是字串末尾有没有'\0',这个取决于编译器
cout << pstr_17 << endl;
/**************************************************/
//string 返回某个字符串
string str_18("string_18");
string str_18_1 = str_18.substr(3); //返回下标3以后的字符串
cout << str_18_1 << endl;
/**************************************************/
//string的查找函数 find和rfind
string str_19("string_19");
int ipos = str_19.find('r',0); //从0的位置开始寻找,找到了就返回下标
cout << ipos << endl; //输出2
int irpos = str_19.rfind("ri"); //反向查找
cout << irpos << endl; //输出2
//find_first_of find_last_of
int iffpos = str_19.find_first_of("ing");
cout << iffpos << endl; //输出3
int iflpos = str_19.find_last_of("g_");
cout << iflpos << endl; //输出6
//find_first_not_of find_last_not_of
int iffnopos = str_19.find_first_not_of("our"); //返回第一次不一样的字符的下标
cout << iffnopos << endl; //输出0
system("pause");
return 0;
}