C++ Vector

Warning: careful. My English is poor.

How to access it in C++?

#include < vector >
Then you can use it.

Basic Initalization
ConstructorResult
vector cGet a Empty vector
vector c(c2)Get a vector as same as c2, exactly same
vector c = c2Get 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)

OperationResult
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
OperationResult
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 == c2compare c1 with c2, all elements, same index
c1 > c2 / c2 > c1 …… (You know it)
Assignment Operation
OperationResult
c = c2give 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.
OperationResult
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
OperationResult
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值