欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++ Vector

程序员文章站 2022-03-23 15:00:27
...

Warning: careful. My English is poor.

How to access it in C++?

#include < vector >
Then you can use it.

Basic Initalization

Constructor Result
vector c Get a Empty vector
vector c(c2) Get a vector as same as c2, exactly same
vector c = c2 Get a vector has all elements of c2
vector c(n) Get a size n vector
vector c(n, elem) Get a size n vector and it’s filled by elem
vector c(beg, end) Get a vector, the elements of the vector is [beg, end)
c.~vector() destroy all elements and release all memory

How to access a vector

At first, it’s a array in C++. O(1)

Operation Result
c[index] Get the value in vector which index is index, but it won’t check whether the index is legal or not
c.at(index) Same as c[index], but it will check the index, if it’s illegal, throws range-error
c.front() Get the first element of the vector, won’t check there has or hasn’t the first element
c.back() Get the last element of the vector, won’t check too

Basic Operations

Operation Result
c.empty() Check whether the vector is empty or not. return Bool
c.size() Get the number of the elements in this vector
c.max_size() Get the potential of memory, how many elements you can save theoretically
c.capacity() Get the max capacity of vector without redistribution
c.reserve(num) If you don’t have enough memory for saving element, give it more
c1 == c2 compare c1 with c2, all elements, same index
c1 > c2 / c2 > c1 … … (You know it)

Assignment Operation

Operation Result
c = c2 give all the elements of c2 to c
c.assign(n, elem) copy the elem n times and give them to c
c.swap(c2) give all elements of c2 to c, and give all elements of c to c2
swap(c1, c2) give the elements of c2 to c1, and …

Random-access Iterator

The iterator always effective, unless…

  1. Insert or Remove a element at a index smaller.
  2. Redistribution because there’s no enough memory.
Inserting and Removing

According to the STL, you should give a legal parameter to function.
so

  1. Iterator should point at a legal index
  2. The start pointer can’t bigger than the end index.
Operation Result
c.begin() return a random-access iterator points at the first element
c.end() return a random-access iterator points at the last element
c.cbegin() return a const random-access iterator points at the first element C++11
c.cend() return a const random-access iterator points at the last element C++11
c.rbegin() return a reverse iterator points at the first element in a reverse direction. Actually the last element.
c.rend() like the c.rbegin()'s meaning, same as c.begin()
c.crbegin() you know it, const reverse random-access … C++11
c.crend() I think you know it now. C++11
More
Operation Result
c.push_back(elem) add a element to the end
c.pop_back() remove the last element
c.insert(pos, elem) insert a element before pos, return the index.
c.insert(pos, n, elem) insert elements(n) before pos, return the index of first element
c.insert(pos, beg, end) copy the elements between beg and end insert them before pos, then return the the index of first element
c.insert(pos, initlist) you know it, return the index of first element C++11
c.emplace(pos, args…) C++11
c.emplace_back(args…) you know it , return nothing C++11
c.erase(pos) remove the element at pos, return the index of next element
c.erase(beg, end) remove the element between beg and end, return the index of next element
c.resize(num)
c.resize(num, elem)
c.clear() remove all elements
Example
  1. remove the value
std::vector<Elem> v;
std::vector<Elem>::iterator pos;
pos = find(v.begin(), v.end(), val);
if (pos != v.end())
{
	v.erase(pos);
}
  1. More
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	vector<string> sentence;
	sentence.reserve(5);
	sentence.push_back("Hello,");
	sentence.insert(sentence.end(), {"how", "are", "you", "?"});
	copy(sentence.cbegin(), sentence.cend(), ostream_iterator<string>(cout, " "));
	cout << endl;

	cout << " max_size(): " << sentence.max_size() << endl;
	cout << " size():" << sentence.size() << endl;
	cout << " capacity():" << sentence.capacity() << endl;

	swap(sentence[1], sentence[3]);

	sentence.insert(find(sentence.begin(), sentence.end(), "?"), "always");

	sentence.back() = "!";

	copy(sentence.cbegin(), sentence.cend(), ostream_iterator<string>(cout, " "));
	cout << endl;

	cout << " size(): " << sentence.size() << endl;
	cout << " capacity(): " << sentence.capacity() << endl;

	sentence.pop_back();
	sentence.pop_back();
	sentence.shrink_to_fit();

	cout << " size():  " << sentence.size() << endl;
	cout << " capacity():  " << sentence.capacity() << endl;

	return 0;
}
Actual combat

LeetCode3: Longest Substring Without Repeating Characters

LightMark’s solution

Here is code.

int lengthOfLongestSubstring(string s)
{
	vector<int> dict(256, -1);
	int maxLen = 0, start = -1;
	for (int i = 0; i != s.length(); i++)
	{
		if (dict[s[i]] > start)
			start = dict[s[i]];
		dict[s[i]] = i;
		maxLen = max(maxLen, i - start);
	}
	return maxLen;
}