/*********************/
/* demo string */
/*********************/
#define _SCL_SECURE_NO_WARNINGS
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
//构造仿函数:把1变为0,非0变为1
struct MyStruct
{
char operator()(char& obj)
{
if (obj == '0') return '1';
else return '0';
}
};
int main()
{
char buf[128];
string str = "abcdefabcdefabc";
//copy
str.append("def");
str = str + "abc";
cout << "str:\t" << str << endl;
//find
int offset = 0;
offset = str.find("abc", offset);
cout << endl;
cout << "offset:\t" << offset << endl;
//count
int count = 0;
while (offset != string::npos)
{
count++;
offset += 3;
offset = str.find("abc", offset);
}
cout << endl;
cout << "count:\t" << count << endl;
//replace
offset = 0;
str.replace(offset, 2, "000");
cout << endl << "replace" << endl;
cout << "str1:\t" << str << endl;
//copy to buf copy(buf, n, offset);
offset = 1;
str.copy(buf, 3, offset);
cout << "buf:\t" << buf[0] << buf[1] << buf[2] << endl;
//erase
string::iterator it = str.begin();
it = find(it, str.end(), 'a');
while (it != str.end())
{
it=str.erase(it);//删除迭代器会把it也删除,但erase函数会返回下一个迭代器位置
it = find(it, str.end(), 'a');
}
cout << endl << "erase" << endl;
cout << "str:\t" << str << endl;
//transform transform(first, end, out, toupper);
MyStruct myfun;
transform(str.begin(), str.end(), str.begin(), myfun);
cout << endl << "transform" << endl;
cout << "str:\t" << str << endl;
system("pause");
return 0;
}