13天带你了解C++ ---DAY10 C++之vector

13天带你了解C++ ---DAY10 C++之vector,第1张

目录

1.string容器

2.构造函数和析构函数的相关 *** 作

3.迭代器

4.容量相关

5.元素访问相关

6.元素遍历相关

7.元素 *** 作相关

 8.vector模拟实现


1.string容器

        vector容器是C++标准模板库提供的管理任意元素的动态顺序表容器。vector容器底层实现和string很像,都是动态的顺序表。也可以说string是vector的一种特例,它将普通的vector容器封装,增加了对字符串的 *** 作,即对‘0’的 *** 作,下边介绍了vector容器的一些基本 *** 作,均参考于C++官方文档。

2.构造函数和析构函数的相关 *** 作
#include
using namespace std;
#include
void TestVector1() {
	vector v1;
	vector v2(10);
	vector v3(3,5);
	vector v4{ 1,2,3,4,5,6,7,8,9,0 };

	string s("hello world");
	vector v5(s.begin(), s.end());

	vector v6(v5);			//拷贝构造
	vector v7=v6;
	
}

int main() {
	TestVector1();

	return 0;
}

3.迭代器
#include
using namespace std;
#include
void TestVector1() {
	vector v4{ 1,2,3,4,5,6,7,8,9,0 };
	string s("hello world");
	vector v5(s.begin(), s.end());

	vector::iterator it = v4.begin();		//正向迭代器
	auto rit = v5.rbegin();						//反向迭代器
	for (it = v4.begin(); it != v4.end(); it++) {
		cout << *it << " " ;
	}
	cout<< endl;

	while (rit != v5.rend()) {
		cout << *rit;
		rit++;
	}
    cout<< endl;

}

int main() {
	TestVector1();
	return 0;
}

4.容量相关
#include
using namespace std;
#include
void TestVector2() {
	vector v4{ 1,2,3,4,5,6,7,8,9,0 };

	int len=v4.size();
	cout << "len:" << len << endl;

	v4.resize(15,11);                     //调整有效元素个数为15,若原对象小于15,则用11补位
	cout << "v4.resize(15,11):";
	for (auto e : v4) 
		cout << e<<" ";
		cout << endl;

	v4.resize(5);                           //调整有效元素个数为5,若原对象大于5,则截断
	cout << "v4.resize(5):";
	for (auto e : v4) 
		cout << e<<" ";
		cout << endl;

	v4.reserve(20);                        //调整capcity大小为20
	cout<<"v4.reserve(20):" << v4.capacity()<

5.元素访问相关

at和[]在使用效果方面完全一致,使用方法举例  cout << str.at(i);

区别:[]在越界访问时会触发assert断言,at会抛出异常,可供用户捕获。

#include
using namespace std;
#include
void TestVector3() {
		vector v1{ 1,2,3,4,5 };
		cout<

6.元素遍历相关

1.下标遍历

2.范围for遍历

3.迭代器遍历

#include
using namespace std;
#include
void TestVector1() {
	vector v4{ 1,2,3,4,5,6,7,8,9,0 };
	string s("hello world");
	vector v5(s.begin(), s.end());

	vector v6(v5);			//拷贝构造
	vector v7=v6;

	vector::iterator it = v4.begin();		//正向迭代器
	auto rit = v5.rbegin();						//反向迭代器
	for (it = v4.begin(); it != v4.end(); it++) { //正向迭代器遍历打印
		cout << *it << " " ;
	}
	cout<< endl;

	while (rit != v5.rend()) {                    //反向向迭代器遍历打印
		cout << *rit;
		rit++;
	}
	cout << endl;

	for (auto e : v4) {							//范围for遍历打印
		cout << e << " ";
	}
	cout << endl;

	for (int i = 0; i 
7.元素 *** 作相关
#include
using namespace std;
#include
void TestVector4() {
		vector v1{ 1,2,3,4,5 };
		v1.push_back(8);			//在顺序表最后插入一个数字
		v1.pop_back();				//删除顺序表的最后一个数字

		auto it = v1.begin();		//删除任意位置的元素
		v1.erase(it);
		
		 it = v1.begin();			//删除指定范围的元素
		 v1.erase(it , it + 2);
		
		auto i = v1.begin();		//在指定位置插入一个元素
		v1.insert(i+2, 99);

		i=v1.begin();				//在你指定位置插入多个元素
		v1.insert(i+2, 5, 88);

	}


int main() {
	TestVector4();
	return 0;
}

 8.vector模拟实现
#include
using namespace std;
namespace vec {
	template
	class vector {
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			:start(nullptr)
			,finish(nullptr)
			,end_of_storage(nullptr)
		{}

		vector(size_t n,T& value=T())
			:start(new T[n])
			,finish(start)
			,end_of_storage(start+n)
		{
			for (int i = 0; i < n; i++) {
				*finish++=value;
			}
		}

		vector(int n, T value = T())
			:start(new T[n])
			, finish(start)
			, end_of_storage(start + n)
		{
			for (int i = 0; i < n; i++) {
				*finish++ = value;
			}
		}

		vector(const vector& v)
			: start(nullptr)
			, finish(nullptr)
			, end_of_storage(nullptr) 
		{
			vector tmp(v.cbegin(),v.cend());
			this->swap(tmp);
		}

		template
		size_t distance(Iterator first, Iterator last) {
			size_t count = 0;
			while (first != last) {
				++count;
				++first;
			}
			return count;
		}



		template
		vector(Iterator first,Iterator last) {
			size_t n = distance(first, last);
			start = new T[n];
			finish = start;
			end_of_storage = start + n;

			while (first != last) {
				*finish = *first;
				first++;
				finish++;
			}
		}

		

		///赋值运算符重载
		vector& operator=(vector v) {
			this->swap(v);
			return *this;
		}

		~vector() {
			if (start) {
				delete[] start;
				start = nullptr;
				finish = nullptr;
				end_of_storage = nullptr;
			}
		}


		///iterator
		iterator begin() {
			return start;
		}

		iterator end() {
			return finish;
		}

		const_iterator cbegin()const
		{
			return start;
		}
		const_iterator cend()const
		{
			return finish;
		}

		iterator rbegin() {
			return end();
		}

		iterator rend() {
			return start;
			
		}

		/容量
		size_t size()const {
			return finish - start;
		}

		size_t capcity() {
			return end_of_storage - start;
		}

		bool empty()const {
			return finish == start;
		}

		void resize(size_t newsize,const T& value=T()) {
			size_t oldsize = size();
			if (newsize <= oldsize) {
				finish = start+newsize;
			}
			else{
				if (newsize > capcity) {
					reserve(newsize);
				}
				memset(finish, value,newsize-oldsize);
				finish = start+newsize;
			}
		}

		void reserve(size_t newcapcity) {
			size_t oldcapcity = capcity();
			size_t n = size();
			if (newcapcity > oldcapcity) {
				T* tem = new T[newcapcity];
				memcpy(tem, start, sizeof(T) * size());
				delete[] start;
				start=tem;
				finish = start + n;
				end_of_storage = start + newcapcity;
			}
		}



		/元素访问相关
		T& operator[] (size_t index) {
			assert(index < size());
			return start[index];
		}
		
		const T& operator[] (size_t index)const
		{
			assert(index < size());
			return start[index];
		}

		T& front() {
			return *begin;
		}
		T& front()const {
			return start[0];
		}

		T& back() {
			return *(finish-1);
		}
		T& back()const {
			return start[finish-1];
		}

		修改相关操作
		void push_back(const T& value) {
			if (finish == end_of_storage) {
				reserve(capcity() * 2+3);
			}
			*finish = value;
			++finish;
		}
		
		void pop_back() {
			if (empty())
				return;

			--finish;
		}

		/insert
		iterator insert(iterator pos, const T& value) {
			if (posfinish) {
				return pos;
			}
			if (finish == end_of_storage) {
				reserve(capcity()*2);
			}
			auto it = finish - 1;
			while (it >= pos) {
				*(it + 1) = *it;
				it--;
			}
			*pos = value;
			finish++;
			return pos;
		}

		iterator erase(iterator pos) {
			if (pos < start || pos >= finish)
				return end();
			auto it = pos + 1;
			while (it& v) {
			std::swap(start, v.start);
			std::swap(finish, v.finish);
			std::swap(end_of_storage, v.end_of_storage);
		}

	private:
		iterator start;
		iterator finish;
		iterator end_of_storage;
	};
}


void TestVector1() {
	vec::vector v1;
	vec::vector v2(10,5);
	vec::vector v3(v2);

	int arr[]{ 1,2,3,4,5,6 };
	vec::vector v4(arr,arr+3);

	for (auto e : v3) {
		cout << e << " ";
	}
	cout< v1(10, 5);
	v1.clear();
	v1.empty();
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.pop_back();
	for (auto e : v1) {
		cout << e << " ";
	}
	cout << endl;

	auto it = v1.begin();
	v1.insert(it+1, 99);
	for (auto e : v1) {
		cout << e << " ";
	}
	cout << endl;

	v1.erase(v1.begin());
	for (auto e : v1) {
		cout << e << " ";
	}
	cout << endl;
}



int main() {
	TestVector1();
	TestVector2();
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/875422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)