Use c ++ --- string class

The basic concept of the container string
common operations vessel string
string little practice

  1. The basic concept of string container
  • c-style string (a character array null character) too difficult to master complex, not suitable for the development of large programs, c ++ standard library define a string class, defined in the header file
    - string and string comparison c style
    char is a pointer, string is a class, string encapsulates char , string management, is a char type container.
    string encapsulates many members of the methods used, such as finding find, copy copy, delete delete, replace replace, insert insert and so on.
    We need to consider the release of memory and cross-border, string char manage
    the allocated memory, assigning each a string, a string by string class is responsible for maintenance, do not worry we assign the value of cross-border and cross-border issues, etc.
    string vessel operating common
    - string constructor
string();//创建一个空的字符串 例如: string str;      
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化

#include <iostream>
#include <string>
using namespace std;

 int main(){
		string s1("abcd");
		char * p = "cdefg";
		string s2(p);
		string s3(s2);
		string s4(4, 'a');
		cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl;
	system("pause");
	return EXIT_SUCCESS;
    }

- string basic assignment

string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符赋值给当前的字符串
string& assign(const char *s);//把字符串s赋给当前的字符串
string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s);//把字符串s赋给当前字符串
string& assign(int n, char c);//用n个字符c赋给当前字符串
string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串

Note: When the copy constructor is our deep copy, rather than a simple shallow copy.
At this point we need to note that the following code

string str = "abcd";

This is not an assignment, this is our copy constructor

  • Character string access operation
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符

Here at and [] There is a difference, at the time of our cross-border atwill throw an exception, but we []are an assertion directly cause the program to crash

  • string splicing operation
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c
  • string Find and Replace
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;  //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const;  //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const;  //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
  • string comparison operation
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较

compare function returns> 1, -1 <, the return == 0.
Case-insensitive comparison, when compared to a reference sequence of the dictionary, the smaller the preceding row.
Uppercase A is smaller than the lowercase a.

  • string substring
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
  • string iterators return
    Here Insert Picture Description
    here and let's begin the discussion of these two types rbegin in c ++ 11 introduced by the new method,
    begin and end is it refers to the location of the first element of the string and the last element the next position
    Here Insert Picture Description
    rbegin and rend precisely and we begin and end is the opposite of
    Here Insert Picture Description
    the commonly used traversal strings, and with the scope for traversing
    actually string iterator is a char * pointer is pointing to our stored under the string corresponding to the target address
#include <iostream>
#include <string>
using namespace std;
int main(){

	string s("asdhijhnf");
	string::iterator it = s.begin();
	string::iterator it2 = s.end();
	while (it != it2){
		cout << *it << " ";
		it++;
	}
	cout << endl;
	for (auto ch : s){
		cout << ch << " ";
	}
	cout << endl;
	system("pause");
	return EXIT_SUCCESS;
}

Here Insert Picture Description

  • string insert and delete operations
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符 
  • and c string pointer type conversion
const char* c_str();//c_str()是一个类的成员函数,其实就是返回类中封装的那个char*指针

char converter to a string type of the constructors of the string class can be char convert to string
exists a const char * to the string implicit type conversion c ++ in, it does not exist a string object to automatic conversion c_string type for string string type, can be converted by the c_str () member function

- string operation capacity

  • size: Returns a valid character string length
  • length: Returns the effective length of the string (and this is the size (), this is a problem left over from history.)
  • capacity: Returns the total size of space
  • empty: determining if a string is an empty string, returns true if the string is empty, otherwise false
  • clear: Empty string
  • Reserve, : a string reserve space
  • a resize (n, char ch = '\ 0') : an effective change of n number, multiple use of space filling ch.
    c ++ string can control their own length of the string, not the user to manage, but the frequent release of open space and space consumption significantly. Preset time we can use our reserve and resize conduct our space
    both are, resize the difference: is the size parameter string class were increased our capacity at this time can not be determined, depending on memory allocation mechanism, look at the size of the allocation. reserve: is carried out to improve our capacity, but our size do not change.
//使用resize进行空间预留
#include <iostream>
#include <string>
using namespace std;
int main(){

	string s;
	s.resize(100);
	for (int i = 0; i < s.size(); i++){
		s[i] = 'a';
	}
	cout << "size=" << s.size() << endl;
	cout << "capacity=" << s.capacity() << endl;
	cout << s << endl;
	system("pause");
	return EXIT_SUCCESS;
}

Here Insert Picture Description
Here capacity is the time when we became 111. resize (111) of.
Here Insert Picture Description
This description is not to reset the space is not only related with our input parameters, but there are some internal memory allocation mechanism. It is to reduce our frequent open space.
When using the reservetime we have to pay attention to use, because at reservethe time sizedoes not change.

#include <iostream>
#include <string>
using namespace std;
int main(){

	string s;
	s.reserve(100);
	for (int i = 0; i < s.size(); i++){
		s[i] = 'a';
	}
	cout << "size=" << s.size() << endl;
	cout << "capacity=" << s.capacity() << endl;
	cout << s << endl;
	system("pause");
	return EXIT_SUCCESS;
}

Here Insert Picture Description
This time will not change size
so in this case the assignment should be changed, using push_back () to perform the data interpolation tail

#include <iostream>
#include <string>
using namespace std;
int main(){

	string s;
	s.reserve(100);
	for (int i = 0; i < 100; i++){
		s.push_back('a');
	}
	cout << "size=" << s.size() << endl;
	cout << "capacity=" << s.capacity() << endl;
	cout << s << endl;
	system("pause");
	return EXIT_SUCCESS;
}

Here Insert Picture Description

3. string a little practice

#include <iostream>
#include <string>
using namespace std;
int main(){

	string s;
	cin >> s;
	int i = 0;
	int j = s.size() - 1;
	while (i < j){
		swap(s[i++], s[j--]);
	}
	cout << s << endl;
	system("pause");
	return EXIT_SUCCESS;
}

Here Insert Picture Description

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void blindst(string& str1, string& str2){
    int flag = 0;
    int i = 0;
    for (; i < str1.length(); i++){
        int sum = str1[i] - '0' + str2[i] - '0'+flag;//得到两个结果的和
        str2[i] = (char)(sum % 10 + '0');
        flag = sum / 10;
    }
    while (i < str2.length()){
        int sum = (str2[i] - '0') + flag;
        str2[i] = (char)(sum % 10 + '0');
        flag = sum / 10;
        i++;
    }
    if (flag != 0){
        str2 += to_string(flag);
    }
}
int main(){
    string str1, str2;
 
    cin >> str1 >> str2;
    vector<string>v;
    int ss = 1;
    for (int i = str1.length() - 1; i >= 0; i--){
        string s = "";
        for (int j = i; j < str1.length() - 1; j++){
            s += "0";
        }
        int flag = 0;
        for (int j = str2.length() - 1; j >= 0; j--){
            int x = (str2[j] - '0')*(str1[i] - '0') + flag;
            int sul = (x ) % 10;
            flag = x / 10;
            s += to_string(sul);
        }
        if (flag != 0){
            s += to_string(flag);
        }
        v.push_back(s);
         
    }
    //将所有的结果合并起来
    for (int i = 0; i < v.size()-1; i++){//从第一个开始向后面合并
        blindst(v[i], v[i + 1]);
    }
    for (int i = v[v.size() - 1].length() - 1; i >= 0; i--){
        cout << v[v.size() - 1][i];
    }
    system("pause");
    return EXIT_SUCCESS;
}

Guess you like

Origin blog.csdn.net/boke_fengwei/article/details/90646913