[C + +] string common interface

1, STL

STL (Standard Template Library): it is an important part of C + + standard library. It is not only a reusable component library, but also a software framework including data structures and algorithms.

2, string

1. Definitions

String is a template class of string

int main()
{
    string s1("hello");//Construction object
    return 0;
}

It can be seen that string is in std standard library and is typedef

Library definition:

template<class T>
class basic_string
{
    pr
}

It can be seen that string is originally a class template, but its template type is * * char**

However, you can't write string directly with char as the type here. To make the template more usable, because there are other string types

The coding is suggested by the mapping relationship of value + symbol - forming a coding table
ascii coding table - indicates the English coding table
unicode -- represents the world text coding table UTF-8 (under Linux) utf-16 utf-32
gbk -- Chinese self-made coding table. Chinese characters are expressed in two bytes

2. Common interfaces

Constructor:

Red is commonly used

Here, the third one can control the length of the copy structure you want,

npos refers to a static variable in a string, which is actually - 1, given size_t. Unsigned integer, - 1 is the largest. In fact, it is to copy the structure to the end

The fifth is to copy and construct the first few characters of the string, and the sixth is to construct with n characters

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

int main()
{
  string s1;//Nonparametric structure
  string s2("abc");//Parameter transmission structure
  string s3(s2);//copy construction 

  cin >> s1;

  cout << s2 << endl;
  cout << s3 << endl;
  cout << s1 << endl;

  string s4(s1);

  cout << s4 << endl;

  return 0;
}

String length:

Does not contain '\ 0'

Better use size

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

int main()
{
	string s1;
	string s2("abc");
	string s3(s2);

	cout << s2.length() << endl;
	cout << s2.size() << endl;


	return 0;
}

Requested space size:

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

int main()
{
	string s1;

	cout << s1.capacity() << endl;

	return 0;
}

Clear data:

Although the data is gone, the space for development is still there

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

int main()
{
	string s1("abc");
	cout << s1 << endl;
	
	s1.clear();
	cout << s1 << endl;
	cout << s1.capacity() << endl;

	return 0;
}

Delete data

In essence, it is a sequence table to minimize header deletion, because it needs to move data

String array:

The two are similar, but the way of checking out of bounds is different. The operator asserts that at throws exceptions

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

int main()
{
	string s1("abc");

	cout << s1 << endl;
	
	s1.clear();
	cout << s1 << endl;

	cout << s1.capacity() << endl;

	return 0;
}

Character:

int main()
{
	string s1;
	s1.push_back('a');
	s1.append("bcde");
	cout << s1 << endl;

	s1 += ':';
	s1 += "hello world";
	cout << s1 << endl;


	return 0;
}

Head and tail insertion is supported, but the efficiency is very low because it is a sequence table

Traversal string:

  • Normal size traversal. Note that the return value is size_t

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

int main()
{
	string s1("abc");

	for (size_t i = 0; i < s1.size(); ++i)
	{
		cout << s1[i];
	}
	
	return 0;
}
  • iterator

Access and modify container

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

int main()
{
	string s1("abc");

    //The iterator is in the class field of string
    /
	string::iterator it = s1.begin();
    //Returns the address of the first data
	while (it != s1.end())//Return the last data address' \ 0 '
	{
		cout << *it;
		++it;
	}
    //Reverse output
    string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *it;
		++it;
	}

	return 0;
}

What is the meaning of iterator traversal?
All containers can use iterators to access modifications
For string, whether it is forward traversal or backward traversal, subscript + [] is easy enough. Why do you need an iterator?
For string s, subscripts and [] are easy enough to use, and iterators can really be avoided.
But what about other containers (data structures)?

  • Range for, syntax sugar
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("abc");

	for (auto input : s1)
	{
		cout << input;
	}

	return 0;
}

Open space:

The opening space here is not necessarily just right. It is generally 1.5-2 times the opening space gradually

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

int main()
{
	string s1("abc");
	s1.reserve(100);

	cout << s1.capacity() << endl;
	return 0;
}

The difference between resize and reserve

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

int main()
{
	string s1("abc");
	s1.reserve(100);

	string s2;
	s2.resize(100);

	return 0;
}

resize initializes

It can be found by comparing the size

Print string:

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

int main()
{
	string s1("abc");
	cout << s1 << endl;
	
	cout << s1.c_str() << endl;

	return 0;
}

The return values of the two are different,

const char* //You can use c_str() gets the address of the string
//example
string s1;
st1.c_str();

Find string:

find returns the position where the first character of the string is found

The search order is from left to right

Here's rfind. Look from right to left

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

int main()
{
	string s1("test.txt");
	cout << s1.find(".txt");

	return 0;
}

Combined with substr

string substr (size_t pos = 0, size_t len = npos) const;//Starting position, length
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("test.txt");
	size_t pos = s1.find('.');
	cout << s1.substr(pos, s1.size() - pos);
	
	return 0;
}

Keywords: C++ Back-end string

Added by galewis on Sun, 06 Mar 2022 08:46:24 +0200