練習題:
這個題的思路是從前往后,從后往前同時找,不是字母的話就繼續,是的話就交換。
代碼:
#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;
}