C++:string 1


练习题:

这个题的思路是从前往后,从后往前同时找,不是字母的话就继续,是的话就交换。



代码:
#define _CRT_SECURE_NO_WARNINGS 1
 #include <iostream>
 #include <string>
 using namespace std;
//1、4个默认成员函数
 void test_string1()
 {
     string s1;
     string s2("hello");
     string s3("hello", 2);
     string s4(s2);
     string s5(s2, 1, 2);
     string s6(s2, 1);
     string s7(10, 'a');
     s1 = s7;
    cout << s1 << endl;
     cout << s2 << endl;
     cout << s3 << endl;
     cout << s4 << endl;
     cout << s5 << endl;
     cout << s6 << endl;
     cout << s7 << endl;
 }
//遍历
 void test_string2()
 {
    string s1("hello");
     s1 += ' ';
     s1 += "world";
     cout << s1 << endl;
    //读  推荐用这种方式
     for (size_t i = 0; i < s1.size(); i++)
     {
         cout << s1[i] << " ";
     }
    //写
     for (size_t i = 0; i < s1.size(); i++)
     {
         s1[i] += 1;
     }
     cout << endl;
    //读
     for (size_t i = 0; i < s1.size(); i++)
     {
         cout << s1[i] << " ";
     }
     cout << endl;
    //迭代器
     //写
     string::iterator it = s1.begin();
     while (it != s1.end())
     {
         *it -= 1;
         ++it;
     }
    //读
     it = s1.begin();
     while (it != s1.end())
     {
         cout << *it << " ";
         ++it;
     }
     cout << endl;
    //范围for
     //C++11 -> 原理是被替换成迭代器
     for (auto ch : s1)
     {
         cout << ch << " ";
     }
     cout << endl;
 }
int string2int(const string& str)
 {
     int val = 0;
     string::const_iterator it = str.begin();
     while (it != str.end())
     {
         //*it = 'a';//const 迭代器不能修改
         val *= 10;
         val += *it-'0';
         ++it;
     }
     return val;
 }
void test_string3()
 {
     string s1("hello world!");
     //倒着往前遍历
     string::reverse_iterator rit = s1.rbegin();
     //读
     while (rit != s1.rend())
     {
         cout << *rit << " ";
         ++rit;
     }
     cout << endl;
    string nums("12345");
     cout << string2int(nums) << endl;
    //方向:正向和反向
     //属性:普通和const
 }
void test_string4()
 {
     string s1("hello world!");
     string s2("hello");
     cout << s1.size() << endl;
     cout << s2.size() << endl;
     cout << s1.length() << endl;
     cout << s2.length() << endl;
    cout << s1.capacity() << endl;
     cout << s2.capacity() << endl;
    s1 += "hehe";
     cout << s1.capacity() << endl;
     s1.clear();//只是清除了内容,并不将capacity置0
     cout << s1 << endl;
     cout << s1.capacity() << endl;
 }
void test_string5()
 {
     string s;
     //s.reserve(100);
     //s.resize(100, 'x');
     size_t sz = s.size();
    cout << "making s grow:\n";
     for (int i = 0; i < 100; i++)
     {
         s.push_back('c');
         if (sz != s.capacity())
         {
             sz = s.capacity();
             cout << "capacity changed:" << sz << endl;
         }
     }
 }
void test_string6()
 {
     //string s;
     //s.push_back('x');
     //s.append("x");
    //s += '1';
     //s += "hello";
//cout << s << endl;
    string s;
     s += '1';
     s += "3456";
     cout << s << endl;
     s.insert(s.begin(), '0');
     cout << s << endl;
     s.insert(2, "2");
     cout << s << endl;
     s.erase(2, 3);
     cout << s << endl;
     s.erase(2, 10);
     cout << s << endl;
 }
 int main()
 {
     //test_string1();
     //test_string2();
     //test_string3();
     //test_string4();
    //test_string5();
     test_string6();
     return 0;
 }
