C++ study notes (2) - vector library articles

What is vector?

vectorIt is a dynamic array class library in C++ STL, which supports random access. Similar to that in Java ArrayList.


How to introduce the vector library?

#include<vector>
using namespace std;

How to create a vector?

Similar to ArrayList in Java, use generics to create a vector that can store data of a specific type.

vector<int> nums;

As in the above code, create a intvector of type.


common method

The following are some common member methods of vector [ 1 ] .

insert element

// 向末尾添加一个元素
void push_back(const T& x)

delete element

// 删除末尾的元素
void pop_back()

// 清空所有元素
void clear()

access element

// 返回pos位置元素的引用
reference at(int pos)

// 返回首元素的引用
reference front()

// 返回尾元素的引用
reference back()

// 数组式的访问方式
nums[index]

modify element

// 设置第n个元素的值为x
void assign(int n,const T& x)

// 数组式的修改方式
nums[index] = newValue;

Find the size

unsigned int size() const

It should be noted that the return value type here unsigned intis unsigned integer.

Empty

// 判断是否为空
bool empty() const

// 通过当前大小
if (nums.size() == 0)

Two-dimensional array

vector<vector<int>> nums;

to sort

To sort the vector, you need to import algorithmthe library and call its sortmethods.

// 默认升序排序
sort(nums.begin(), nums.end());

// 降序排序方法
sort(nums.begin(), nums.end(), greater<int>());

// 另一种升序排序方法
sort(nums.begin(), nums.end(), less<int>());

sortComparatorThe three-parameter overload of can be passed to the comparison method, similar to the comparator in Java . The import functionallibrary can use greaterand less, which represent descending and ascending order respectively. Note that the generic type in angle brackets should be written correctly.


custom sort

Sometimes you need comparison logic other than ascending and descending, so you need a custom comparator [ 2 ] .

more complex structure

struct s
{
    
    
	int a, b;
};

custom comparator

Note that truea condition with a return value represents the s1precedings2

bool comparator(const s &s1, const s &s2)
{
    
    
	return s1.a > s2.a;
}

Sort using a custom comparator

Redefine a svector to store the structure

vector<s> sVector;

to sort

sort(sVector.begin(), sVector.end(), comparator);

A pit encountered in practice

When the vector is empty, a regular judgment

if (0 < nums.size() - 1)

The expected result is false, but the actual running result is true.

After debugging, it is found that nums.size() - 1an unexpected result is obtained, and the output shows that it is 4294967295, ie 2^32-1.

The reason is that sizethe return value type of the vector method is an unsigned integer, which is actually an operation 0 + (-1). Since the negative number is stored in the complement code, -1the binary representation of the negative 11111111 11111111 11111111 11111111number is still 32 1s when summed with 0, and converted to decimal is for 4294967295[ 3 ] .

solution

Coerces the result of the computation to a signed integer [ 4 ] .

if (0 < (int)(nums.size() - 1))

  1. Refer to the rookie tutorial ↩︎

  2. Refer to the blog post "From the simplest sort usage in vector to the sort algorithm for sorting structures after the custom comparison function comp" ↩︎

  3. Refer to the blog post "A small pit for finding the length size()-1 of the C++ STL standard template library vector" ↩︎

  4. Refer to the blog post "Error caused by vector.size()-1" ↩︎

Guess you like

Origin blog.csdn.net/Mr_Megamind/article/details/106179723