C++ study notes five-standard library types vector and string

In addition to some basic types like int and float, C++ also defines a rich standard library of abstract data types. The most important standard library types are string and vector, which define variable-size strings and collections respectively.
1 Standard library string type
1.1 Definition and initialization of string object

//构造函数是一个特殊的成员函数,定义如何初始化该类型的对象
string s1 // 默认构造函数,s1为空串
string s2(s1) //将s2初始化s1的副本
string s3("value") //将s3初始化一个字符串字面值的副本
string s4(n,'c') //将s4初始化为字符串‘c’的n个副本

1.2 Operation of string objects

s.empty()     //如果s为空串,则返回true,否则返回false
s.size()      //返回s中字符的个数

Any variable that stores the result of the size operation of a string must be of type string::size_type. It is especially important not to assign the return value of size to an int variable. (In order to avoid overflow)
2 The standard vector type
vector is a collection of objects of the same type, and each object has a corresponding integer index value. The standard library will be responsible for managing the content related to the storage element, and the vector is called a container. To use vector, you need to quote
#include
using std::vector;
2.1 The definition and initialization of vector objects

vector[T] v1; //vector 保存类型为T的对象。默认构造函数v1为空
vector[T] V2(V1) //V2是v1一个副本
vector[T] v3(n,i) //v3包含n个值为i的元素
vector[T] v4(n);  //v4含有值初始化的元素n个副本

2.2 Value initialization
If the element initialization formula is not specified, the standard library will provide an element initial value for value initialization. The specific value depends on the type of element stored in the vector.

vector<int> fvec(10); //十个元素每个元素都是0
vector<string> svec(10);//十个元素每个元素都是空的

2.3 vector object operations

v.empty   //如果V为空,则返回true,否则返回false
v.size()  //返回V中的元素个数
v.push_back(t) //在V的末尾增加一个值为t的元素

The member function size returns the size_type value defined by the corresponding vector class

vector<int>::size_type

2.4 Add element to vector The
push_back() operation accepts an element value and adds it as a new element to the back of the vector object.
The vector subscripting operations on the objects in the vector are not named, and they can be accessed according to the position of the objects in the vector. Usually use the subscript operator to get the element. Similar to string. But the subscript operation does not add elements

vector<int> ivec;
for(vector<int>::size_type i=0;i != 10;++i)
ivec[i]=i;//这样赋值是错误的,因为ivec是vector对像,下标只能用于获取已存在的元素。
ivec.push_back(i);//正确的赋值方法
//试图对不存在的元素进行下标操作是程序设计过程中严重的错误,可以导致“缓冲区溢出”,一定注意,仅仅能够对确知已存在的元素进行下标操作。

In addition to accessing the elements of the vector container through the subscript, we can also access the elements through the iterator, which is a data type that inspects the elements in the container and traverses the elements.
The standard library provides an iterator type for each type of container. Iterators provide a more general method than subscript operations.

2.5 container iterator type

vector <int> ::iterator iter;   //这条语句定义了一个Iter的变量,它的数据类型是由vector<int>::iterator 类型

The vector iterator type defines some operations to get the element pointed to by the iterator and allows the programmer to move the iterator from one element to another. The following is an example of practicing understanding

# include <iostream>
# include<string>
# include<vector>
using namespace std;
int main()
{
    
    
	int i = 0;
	vector<int> vec(10);
	for (vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++,i++)
	{
    
    
		*iter =i;
		cout << "当前值" << *iter << endl;
		++iter ;
		*iter = i + 1;
		cout << "下一个" << *iter<<endl;

	
	}
	return 0;
}

You can see that the position pointed to by the iterator is moved by incrementing iter. The following are some arithmetic operations of iterators

iter+n iter-n //可以对迭代对象加上或减去一个整型值,这样做将产生一个新的迭代器,其位置在iter所指元素之前(加)或之后(减)n个元素的位置。
iter1-iter2 //计算两个迭代器对象之间的距离,该距离是difference_type的signed类型的值,这里与size_type值相似,也是vector<int> ::difference_type

2.6 begin and end operations

Each container defines a pair of functions named begin and end to return an iterator. If there are elements in the container, the iterator returned by begin points to the first element. The element returned by end does not point to any element of the container, it points to the next element of the last element, it just acts as a sentinel.
The following is a routine to assign a value to a vector container element and print it. Which uses iterator, and the use of size_type

# include <iostream>
# include<string>
# include<vector>
using namespace std;
int main()
{
    
    
	int i = 0;
	vector<int> vec;
	for (vector<int>::size_type i=0;i!= 10;i++)
	{
    
    
		vec.push_back(i);
	   vector<int>::iterator iter=vec.begin();
	   iter = iter + i;
	   cout << "the value is " << *iter << endl;

	
	}
	return 0;
}

Supplement, the operation of container size

c.szie() //返回容器C中的元素个数,返回类型为 C::size_type
c.max_size() //返回容器C中可容纳的最多元素个数
c.empty() //返回标记容器大小是否为0的布尔值
c.resize(n) //调整容器c长度大小,实施能够容纳n个元素,如果<c.size() ,则删除多余的元素,否则添加采用值初始化的新元素
c.resize(n,t) //调整容器c的大小,使其能够容纳n个元素,所有新添加的元素都为t
//要注意在使用resize时防止迭代器失效

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/108698038