#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
str = "abcdefg higklmn";
string::iterator beginIter = str.begin();
string::iterator endIter = str.end();
string::reverse_iterator rbeginIter = str.rbegin();
string::reverse_iterator rendIter = str.rend();
cout << *beginIter << endl;
cout << *(endIter - 1) << endl;
cout << *rbeginIter << endl;
cout << *(rendIter - 1) << endl;
for (string::iterator iter = beginIter; iter != endIter; ++iter)
cout << *iter;
cout << endl;
for (string::reverse_iterator rev_iter = rbeginIter; rev_iter != rendIter; ++rev_iter)
cout << *rev_iter;
cout << endl;
int size = str.size();
int length = str.length();
cout << size << " " << length << endl;
int maxSize1 = str.max_size();
cout << maxSize1 << endl;
cout << str.capacity() << endl;
string s2;
cout << s2.capacity() << endl;
bool isEmpty = str.empty();
if (isEmpty) cout << "为空!" << endl;
else cout << "不为空!" << endl;
string s3;
isEmpty = s3.empty();
if (isEmpty) cout << "为空!" << endl;
else cout << "不为空!" << endl;
cout << str[0] << endl;
for (int i = 0; i < str.size(); ++i)
cout << str[i];
cout << endl;
const char* c;
string s4 = "1234";
c = s4.c_str();
cout << c << endl;
s4 = "abcd";
cout << c << endl;
const char* c1;
string s5 = "1234";
c1 = s5.data();
cout << c1 << endl;
s5 = "abcd";
cout << c1 << endl;
string s6 = "hahahaha";
string s7;
s7 = s6;
cout << s7 << endl;
string s8 = "1234";
cout << s8.capacity() << endl;
s8.reserve(32);
cout << s8.capacity() << endl;
string s9 = "aaa";
string s10 = "bbb";
swap(s9, s10);
cout << s9 << " " << s10 << endl;
string s11 = "123";
reverse(s11.begin(), s11.end());
cout << s11 << endl;
reverse(s11.begin(), s11.begin() + 3);
cout << s11 << endl;
string s12 = "aaaaaaaaa";
char* s13 = "bbb";
s12 = s12.insert(0, s13);
s12 = s12.insert(1, s13, 2);
string s14 = "ccc";
s12 = s12.insert(3, s14);
string s15 = "dddde";
s12 = s12.insert(5, s15, 2, 3);
s12 = s12.insert(6, 3, 'f');
string::iterator iter = s12.begin();
iter++;
string::iterator iter2 = s12.insert(iter, 'g');
cout << *iter2 << endl;
cout << s12 << endl;
string s16("Hello ");
const char* c3 = "Out There ";
s16.append(c3);
cout << s16 << endl;
string s17("Hello ");
const char *c4 = "Out There ";
s17.append(c4, 3);
cout << s17 << endl;
string s18("Hello "), s19("Wide "), s20("World ");
s18.append(s19);
s18 += s20;
cout << s18 << endl;
string s21("Hello "), s22("Wide World ");
s21.append(s22, 5, 5);
cout << s21 << endl;
string str1f("Hello "), str2f("Wide World");
str1f.append(str2f.begin() + 5, str2f.end());
cout << str1f << endl;
string str1e("Hello ");
str1e.append(4, '!');
cout << str1e << endl;
str1e.push_back(')');
cout << str1e << endl;
str1e.erase(0, 2);
cout << str1e << endl;
str1e.erase(str1e.begin());
cout << str1e << endl;
str1e.erase(str1e.begin() + 3, str1e.end() - 2);
cout << str1e << endl;
str1e.clear();
cout << str1e.size() << endl;
str1e.resize(10);
cout << str1e.length() << endl;
char* c5 = "aaa";
string s23;
s23.assign(c5);
cout << s23 << endl;
char* c6 = "bbbbbb";
s23 = s23.assign(c6, 3);
cout << s23 << endl;
string s24 = "ccc";
s23 = s23.assign(s24);
cout << s23 << endl;
s23.assign(3, 'd');
cout << s23 << endl;
s23 = s23.assign("eeee", 0, 3);
cout << s23 << endl;
string s25 = "fff";
s23 = s23.assign(s25.begin(), s25.end());
cout << s23 << endl;
string line1 = "this@ is@ a test string!";
line1 = line1.replace(line1.find("@"), 1, "");
cout << line1 << endl;
string line2 = "this@ is@ a test string!";
line2 = line2.replace(line2.begin(), line2.begin() + 6, "heihei ");
cout << line2 << endl;
string line3 = "this@ is@ a test string!";
string substr = "12345";
line3 = line3.replace(0, 5, substr, substr.find("1"), 3);
cout << line3 << endl;
string line4 = "this@ is@ a test string!";
char* str111 = "12345";
line4 = line4.replace(0, 5, str111);
cout << line4 << endl;
string line5 = "this@ is@ a test string!";
char* str222 = "12345";
line5 = line5.replace(line5.begin(), line5.begin() + 9, str222);
cout << line5 << endl;
string line6 = "this@ is@ a test string!";
char* str333 = "12345";
line6 = line6.replace(0, 9, str333, 4);
cout << line6 << endl;
string line7 = "this@ is@ a test string!";
char* str444 = "12345";
line7 = line7.replace(line7.begin(), line7.begin() + 9, str444, 4);
cout << line7 << endl;
string line8 = "this@ is@ a test string!";
char c111 = '1';
line8 = line8.replace(0, 9, 3, c111);
cout << line8 << endl;
string line9 = "this@ is@ a test string!";
char c222 = '1';
line9 = line9.replace(line9.begin(), line9.begin() + 9, 3, c222);
cout << line9 << endl;
string s111("1a2b3c4d5e6f7jkg8h9ija2b3c4d5e6f7g8ha9i");
string::size_type position;
position = s111.find("jk", 0);
if (position != string::npos) cout << position << endl;
else cout << "Can't find!" << endl;
string flag = "c";
position = s111.find_first_of(flag);
printf("s.find_first_of(flag) is :%d\n", position);
position = s111.find_last_of(flag);
printf("s.find_last_of(flag) is :%d\n", position);
position = s111.find("b", 5);
cout << "s111.find(b,5) is : " << position << endl;
position = s111.rfind("c");
if (position != string::npos) cout << position << endl;
flag = "a";
position = 0;
int i = 1;
while ((position = s111.find(flag, position)) != string::npos)
{
cout << "position " << i << " : " << position << endl;
position++;
i++;
}
string x = "Hello_World";
cout << x.substr() << endl;
cout << x.substr(5) << endl;
cout << x.substr(0, 5) << endl;
string str1("green apple");
string str2("red apple");
string str3("apple");
if (str3.compare("apple") == 0)
cout << str3 << " is an apple!" << endl;
if (str1.compare(str2) != 0)
cout << str1 << " is not " << str2 << endl;
if (str1.compare(6, 5, "apple") == 0)
cout << "still, " << str1 << " is an apple!" << endl;
if (str2.compare(str2.size() - 5, 5, "apple") == 0)
cout << "and " << str2 << " is also an apple!" << endl;
if (str1.compare(6, 5, str2, 4, 5) == 0)
cout << "therefore, both are apples!" << endl;
string a("aBcdef");
string b("AbcdEf");
string c123("123456");
string d("123dfg");
int m = a.compare(b);
int n = a.compare(1, 5, b);
int p = a.compare(1, 5, b, 4, 2);
int q = c123.compare(0, 3, d, 0, 3);
cout << "m=" << m << ",n=" << n << ",p=" << p << ",q=" << q << endl;
system("pause");
return 0;
}
