C++ 标准模板库-vector

文章目录

  • 1. 概念
  • 2. Construct(构造)
  • 3. destructor(析构)
  • 4. operator=(重载=符号)
  • 5. Capacity:
  • 6. 参考文献

1. 概念

vector是一个序列化容器,相当于可以改变大小的数组。

2. Construct(构造)

#include 
# include 

int main() {

	//Construct
	// empty vector of ints
	std::vector first;
	// four ints with value 100
	std::vector second(4, 100);
	// iterating through second
	std::vector third(second.begin(), second.end());
	// a copy of third
	std::vector fourth(third);

	int myint[]={16,2,3};
	std::vector fifth(myint,myint+sizeof(myint)/sizeof(int));


	// first
	printf("first============================================= \n");
	printf("sizeof(first)=%d \n", sizeof(first));
	printf("first.size()=%d \n", first.size());
	printf("first.begin()=%d \n", first.begin());
	printf("first.end()=%d \n", first.end());
	std::cout << "first contains:";
	for (std::vector::iterator it = first.begin(); it != first.end();++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	// second
	printf("second============================================= \n");
	printf("sizeof(first)=%d \n", sizeof(second));
	printf("second.size()=%d \n", second.size());
	printf("second.begin()=%d \n", second.begin());
	printf("second.end()=%d \n", second.end());
	std::cout << "second contains:";
	for (std::vector::iterator it = second.begin(); it != second.end();++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	// third
	printf("third============================================= \n");
	printf("sizeof(first)=%d \n", sizeof(third));
	printf("third.size()=%d \n", third.size());
	printf("third.begin()=%d \n", third.begin());
	printf("third.end()=%d \n", third.end());
	std::cout << "third contains:";
	for (std::vector::iterator it = third.begin(); it != third.end();++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	// fourth
	printf("fourth============================================= \n");
	printf("sizeof(first)=%d \n", sizeof(fourth));
	printf("fourth.size()=%d \n", fourth.size());
	printf("fourth.begin()=%d \n", fourth.begin());
	printf("fourth.end()=%d \n", fourth.end());
	std::cout << "fourth contains:";
	for (std::vector::iterator it = fourth.begin(); it != fourth.end();++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	// fifth
	printf("fifth============================================= \n");
	printf("sizeof(first)=%d \n", sizeof(fifth));
	printf("fifth.size()=%d \n", fifth.size());
	printf("fifth.begin()=%d \n", fifth.begin());
	printf("fifth.end()=%d \n", fifth.end());
	std::cout << "fifth contains:";
	for (std::vector::iterator it = fifth.begin(); it != fifth.end();++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	return 0;
}

输出结果

first============================================= 
sizeof(first)=24 
first.size()=0 
first.begin()=0 
first.end()=0 
first contains:
second============================================= 
sizeof(first)=24 
second.size()=4 
second.begin()=1906512 
second.end()=1906528 
second contains: 100 100 100 100
third============================================= 
sizeof(first)=24 
third.size()=4 
third.begin()=1906544 
third.end()=1906560 
third contains: 100 100 100 100
fourth============================================= 
sizeof(first)=24 
fourth.size()=4 
fourth.begin()=1907392 
fourth.end()=1907408 
fourth contains: 100 100 100 100
fifth============================================= 
sizeof(first)=24 
fifth.size()=3 
fifth.begin()=1907424 
fifth.end()=1907436 
fifth contains: 16 2 3

3. destructor(析构)

会自动销毁容器对象。

4. operator=(重载=符号)

vector中重写了操作符=,用法如下:

#include 
#include 

int main() {

	std::vector foo(3, 0);
	std::vector bar(5, 0);

	bar = foo; //将foo的结构及数据赋给bar
	foo = std::vector(); //将新构造的容器赋给foo

	std::cout << "Size of foo: " << int(foo.size()) << '\n';

	std::cout << "\n";
	std::cout << "Size of bar: " << int(bar.size()) << '\n';
	std::cout << "bar contains:";
	for (std::vector::iterator it = bar.begin(); it != bar.end(); ++it) {
		std::cout << ' ' << *it;
	}
	std::cout << '\n';

	return 0;
}

输出结果

Size of foo: 0

Size of bar: 3
bar contains: 0 0 0

5. Capacity:

begin: 返回容器中第一个元素的迭代器。
end: 返回容器中最后一个元素后的迭代器,值是最后一个元素迭代器+1。如果容器是空的,那返回值和begin是一样的。
size: 返回容器大小,即容器中元素的个数。
max_size: 返回容器所能容纳元素的最大数。

#include 
#include 

int main() {

	std::vector myvector;
	for (int i = 1; i <= 5; i++)
		myvector.push_back(i);

	std::cout << "myvector contains:";
	for (std::vector::iterator it = myvector.begin(); it != myvector.end();
			++it)
		std::cout << ' ' << *it;
	std::cout << '\n';

	std::cout << "myvector  size:";
	std::cout << ' ' << myvector.size();
	std::cout << '\n';

	std::cout << "myvector max_size:";
	std::cout << ' ' << myvector.max_size();
	std::cout << '\n';

	return 0;
}

输出结果

myvector contains: 1 2 3 4 5
myvector  size: 5
myvector max_size: 4611686018427387903

6. 参考文献

vector官网

你可能感兴趣的:(C++,c++,算法,蓝桥杯)