vector 内部方法大全 学习(初学者的参考资料)
1 vector构造函数:也就是如何对一个vector对象进行初始化
代码//
explicit vector ( const Allocator& = Allocator() );
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
template <class InputIterator>
vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
vector ( const vector<T,Allocator>& x );
代码//
参数解释:n---初始化元素个数
T---你所要添加进去的元素
first---容器第一个元素
last---容器最后一个元素
x---vector的一个引用
示例:
// constructing vectors
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
// constructors used in the same order as described above:
vector<int> first; // 初始化一个int类型的空vector变量
vector<int> second (4,100); // 初始化4个100
vector<int> third (second.begin(),second.end());
// 通过second 进行赋值
vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
cout << "The contents of fifth are:";
for (i=0; i < fifth.size(); i++)
cout << " " << fifth[i];
cout << endl;
return 0;
}
输出结果:
The contents of fifth are: 16 2 77 29
2 vector::~vector 析构函数
调用每一个元素对象的析构函数
下面是成员函数
3 vector::assign
template <class InputIterator>
void assign ( InputIterator first, InputIterator last );
void assign ( size_type n, const T& u );
如 c.assign(beg,end) //将[beg; end)区间中的数据赋值给c
c.assign(n,elem) //将n个elem的拷贝赋值给c
示例:
// vector assign
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> first;
vector<int> second;
vector<int> third;
first.assign (7,100); // a repetition 7 times of value 100
vector<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
cout << "Size of third: " << int (third.size()) << endl;
return 0;
}
输出结果
Size of first: 7
Size of second: 5
Size of third: 3
4 vector::at
const_reference at ( size_type n ) const;
reference at ( size_type n ); ---返回 vector 内的第n个值,相当于数组下标,从0开始
代码示例
// vec.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector(10);//初始化10个为0的元素。
unsigned int i;
for (i = 0; i < myvector.size(); i ++)
{
myvector.at(i)=i;
}
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector.at(i);
cout << endl;
return 0;
}
5 vector::back
//返回最后一个元素,而不检查该元素是否存在
Returns a reference to the last element of the vector.
reference back( ); const_reference back( ) const;
Return Value
The last element of the vector. If the vector is empty, the return value is undefined.
Remarks
If the return value of back is assigned to a const_reference, the vector object cannot be modified. If the return value of back is assigned to a reference, the vector object can be modified.
Example
// vector_back.cpp // compile with: /EHsc #include <vector> #include <iostream>int main( ) {using namespace std; vector <int> v1;v1.push_back( 10 );v1.push_back( 11 );int& i = v1.back( );const int& ii = v1.front( );cout << "The last integer of v1 is " << i << endl;i--;cout << "The next-to-last integer of v1 is "<< ii << endl; }
Output
The last integer of v1 is 11 The next-to-last integer of v1 is 10
6 vector::begin
iterator begin ();
const_iterator begin () const;
示例
// vector::begin
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
for (int i=1; i<=5; i++)
myvector.push_back(i);
vector<int>::iterator it;
cout << "myvector contains:";
for ( it=myvector.begin() ; it < myvector.end(); it++ )
cout << " " << *it;
cout << endl;
return 0;
}
输出:
myvector contains: 1 2 3 4 5
7 vector::capacity
返回vector对象存储空间大小
例子:
// comparing size, capacity and max_size
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++)
myvector.push_back(i);
cout << "size: " << (int) myvector.size() << "\n";
cout << "capacity: " << (int) myvector.capacity() << "\n";
cout << "max_size: " << (int) myvector.max_size() << "\n";
return 0;
}
A possible output for this program could be:
size: 100
capacity: 141
max_size: 1073741823
8 vector::clear
清除对象内的所有元素,就是调用了析构函数,清空,然后为0
示例
// clearing vectors
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
vector<int> myvector;
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++) cout << " " << myvector[i];
myvector.clear();
myvector.push_back (1101);
myvector.push_back (2202);
cout << "\nmyvector contains:";
for (i=0; i<myvector.size(); i++) cout << " " << myvector[i];
cout << endl;
return 0;
}
8 vector::empty
bool empty () const; 判断vector对象时否为空。
示例:
// vector::empty
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int sum (0);
for (int i=1;i<=10;i++) myvector.push_back(i);
while (!myvector.empty())
{
sum += myvector.back();
myvector.pop_back();
}
cout << "total: " << sum << endl;
return 0;
}
9 vector::end
iterator end ();
const_iterator end () const;
Returns an iterator referring to the past-the-end element in the vector container.
Both iterator and const_iterator are member types. In the vector class template, these are random access iterators
// test_vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myvec;
for (int i=0;i<6;i++)
{
myvec.insert(myvec.end(),i);
}
cout<<"element";
vector<int>::iterator it;
for (it=myvec.begin();it<myvec.end();it++)
{
cout<<" "<<(*it)<<endl;
}
return 0;
}
10 vector::erase
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );
删除vector对象里的一个元素或者一个范围内的元素([first,last)).
示例:
// erasing from vector
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
vector<unsigned int> myvector;
// set some values (from 1 to 10)
for (i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
Output:
myvector contains: 4 5 7 8 9 10
11 vector::front
reference front ( );
const_reference front ( ) const;
Returns a reference to the first element in the vector container.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
myvector.push_back(78);
myvector.push_back(16);
// now front equals 78, and back 16
myvector.front() -= myvector.back();
cout << "myvector.front() is now " << myvector.front() << endl;
return 0;
}
Output:
myvector.front() is now 62
12 vector::get_allocator
allocator_type get_allocator() const;
// test_vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myvector;
int* p;
unsigned int i;
p=myvector.get_allocator().allocate(5);
for (int i=0;i<5;i++)
{
p[i]=i;
}
cout << "The allocated array contains:";
for (i=0; i<5; i++) cout << " " << p[i];
cout << endl;
myvector.get_allocator().deallocate(p,5);
return 0;
}
The allocated array contains: 0 1 2 3 4
13 vector::insert
iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputI last );
// test_vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myvector(3,100);
vector<int> ::iterator it;
it=myvector.begin();
it=myvector.insert(it,200);
myvector.insert(it,2,300);
it=myvector.begin();
vector<int> anothervector(2,400);
myvector.insert(it+2,anothervector.begin(),anothervector.end());
int array[]={501,502,503};
myvector.insert(myvector.begin(),array,array+3);
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin();it<myvector.end();it++)
{
cout<<" "<<*it;
}
return 0;
}
Output:
myvector contains: 501 502 503 300 300 400 400 200 100 100 100
14 vector::max_size
size_type max_size () const;
返回改容器能持有的最大元素数,看一下例子,查看区别
// comparing size, capacity and max_size
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(i);
cout << "size: " << myvector.size() << "\n";
cout << "capacity: " << myvector.capacity() << "\n";
cout << "max_size: " << myvector.max_size() << "\n";
return 0;
}
A possible output for this program could be:
size: 100
capacity: 141
max_size: 1073741823
15 vector::operator=
vector<T,Allocator>& operator= (const vector<T,Allocator>& x);
重载“=”操作符
// vector assignment
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> first (3,0);
vector<int> second (5,0);
second=first;
first=vector<int>();
cout << "Size of first: " << int (first.size()) << endl;
cout << "Size of second: " << int (second.size()) << endl;
return 0;
}
输出结果:
Size of first: 0
Size of second: 3
16 vector::operator[]
reference operator[] ( size_type n );
const_reference operator[] ( size_type n ) const;
Returns a reference to the element at position n in the vector container.
返回vector容器内位置为n的元素的引用
和at()函数相同的功能,就是at()不在范围内会抛异常
示例
// test_vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myvector(10);
unsigned int i;
vector<int> ::size_type size=myvector.size();
for (i=0;i<10;i++)
{
myvector[i]=i;
}
for (i=0;i<size/2;i++)
{
int temp=myvector[size-1-i];
myvector[size-1-i]=myvector[i];
myvector[i]=temp;
}
for (i=0;i<10;i++)
{
cout<<" "<<myvector[i];
}
cout<<endl;
return 0;
}
Output:
myvector contains: 9 8 7 6 5 4 3 2 1 0
17 vector::pop_back
void pop_back ( );
移出元素
示例
// vector::pop_back
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int sum (0);
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty())
{
sum+=myvector.back();
myvector.pop_back();
}
cout << "The elements of myvector summed " << sum << endl;
return 0;
}
输出结果:The elements of myvector summed 600
18 vector::push_back
void push_back ( const T& x );
在末尾处添加一个元素
// vector::push_back
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int myint;
cout << "Please enter some integers (enter 0 to end):\n";
do {
cin >> myint;
myvector.push_back (myint);
} while (myint);
cout << "myvector stores " << (int) myvector.size() << " numbers.\n";
return 0;
}
19 vector::rbegin
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
按照反序迭代输出
示例
// test_vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myvector(10);
unsigned int i;
vector<int> ::size_type size=myvector.size();
for (i=0;i<10;i++)
{
myvector[i]=i;
}
vector<int>::reverse_iterator it;
for (it=myvector.rbegin();it<myvector.rend();it ++)
{
cout<<" "<<*it;
}
cout<<endl;
return 0;
}
输出结果:9 8 7 6 5 4 3 2 1 0
20 vector::rend
public member function
reverse_iterator rend();
const_reverse_iterator rend() const;
代码 同上
21 vector::reserve
void reserve ( size_type n );
Request a change in capacity
Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements.
示例
// vector::reserve
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main ()
{
vector<int> content;
size_t filesize;
ifstream file ("test.bin",ios::in|ios::ate|ios::binary);
if (file.is_open())
{
filesize=file.tellg();
content.reserve(filesize);
file.seekg(0);
while (!file.eof())
{
content.push_back( file.get() );
}
// print out content:
vector<int>::iterator it;
for (it=content.begin() ; it<content.end() ; it++)
cout << hex << *it;
}
return 0;
}
22 vector::resize
void resize ( size_type sz, T c = T() );
// resizing vector
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
unsigned int i;
// set some initial content:
for (i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
cout << "myvector contains:";
for (i=0;i<myvector.size();i++)
cout << " " << myvector[i];
cout << endl;
return 0;
}
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
23 vector::size
size_type size() const;
返回元素个数
示例:
// vector::size
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<10; i++) myints.push_back(i);
cout << "1. size: " << (int) myints.size() << endl;
myints.insert (myints.end(),10,100);
cout << "2. size: " << (int) myints.size() << endl;
myints.pop_back();
cout << "3. size: " << (int) myints.size() << endl;
return 0;
}
Output:
0. size: 0
1. size: 10
2. size: 20
3. size: 19
24 vector::swap
交换vector对象里的数据
// swap vectors
#include <iostream>
#include <vector>
using namespace std;
main ()
{
unsigned int i;
vector<int> first (3,100); // three ints with a value of 100
vector<int> second (5,200); // five ints with a value of 200
first.swap(second);
cout << "first contains:";
for (i=0; i<first.size(); i++) cout << " " << first[i];
cout << "\nsecond contains:";
for (i=0; i<second.size(); i++) cout << " " << second[i];
cout << endl;
return 0;
Output:
first contains: 200 200 200 200 200
second contains: 100 100 100
参考链接:http://msdn.microsoft.com/en-us/library/7df54s3x(v=vs.71).aspx
转载于:https://www.cnblogs.com/followyourdream/p/3292086.html
vector 内部方法大全 学习(初学者的参考资料)相关推荐
- Python学习手册之内部方法、操作符重载和对象生命周期
在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...
- IT 巡检内容、方法大全
IT 巡检内容.方法大全 目 录 1. 概述 2. 巡检维度 3. 巡检内容 4. 巡检方法 5. 常用命令.常见问题和解决方法 6. 附录 1 词汇表 7. 附录 2 参考资料 1. ...
- 网络营销方法大全分享
网络营销方法大全分享 网络营销方法目前至于网商们再熟悉不过了,方法非常简单;目前我们要在简单的方法上做一创意,方可起到事半功倍的成效,要不然将会事倍功半;我们要学会发觉"新大陆", ...
- 关于AVR单片机熔丝位的设置和拯救方法大全 AVR单片机熔丝位的设置和详细的拯救方法
原文: 关于AVR单片机熔丝位的设置和拯救方法大全 AVR单片机熔丝位的设置和详细的拯救方法 熔丝位是ATMEL公司AVR单片机比较独到的特征.在每一种型号的AVR单片机内部都有一些特定含义的熔丝位, ...
- 【Python SMTP/POP3/IMAP】零基础也能轻松掌握的学习路线与参考资料
Python是一种高级编程语言,广泛应用于Web开发.人工智能.数据科学.自动化等领域.SMTP/POP3/IMAP是与邮件相关的三个协议,分别用于发送邮件.接收邮件和访问邮件.使用Python可以轻 ...
- SVO中 Inverse Compositional Image Alignment方法的学习笔记
SVO中 Inverse Compositional Image Alignment方法的学习笔记 这篇文章 光流法简介 逆向光流法 结尾 这篇文章 在SVO系统中的"Relaxation ...
- mysql删除重复文章标题_MySQL中查询、删除重复记录的方法大全
前言 本文主要给大家介绍了关于MySQL中查询.删除重复记录的方法,分享出来供大家参考学习,下面来看看详细的介绍: 查找所有重复标题的记录: 一.查找重复记录 1.查找全部重复记录 2.过滤重复记录( ...
- 生成osm文件_超酷城市肌理!地理数据信息爬取方法大全(B篇)DEM+POI+OSM
WENWEN:这一弹是对第一弹的补充和深化讲解,上一弹请点击常用的地理数据信息爬取方法大全(前期场地信息获取第一弹),关于DEM获取地形地理空间数据云提交任务一直在排队的问题,这个应该是官网的问题,不 ...
- python内点法_Python_Day_02 str内部方法总结
刚开始学习Python,看了一天的字符串内部方法,现在来总结一下. capitalize(self) 将一句话的首字母变大写,其他字母都变小 1 name = "love PyThon&qu ...
最新文章
- 微软已经宣布自2009年4月14日起放弃对windows xp的主流支持
- 时间序列分析模型:ARIMA模型和SARIMAX算法
- micropython文件上传软件_ESP32玩转MicroPython(二) 连接WIFI网络 webperl文件传输
- VMware发布虚拟云网络创新技术,连接和保护分布式多云企业
- sql 大数据量插入优化
- HTML Table 固定列宽,实现excel表格效果
- kettle使用记录
- linux查看网卡的驱动命令行,linux查看网卡驱动模块信息
- mysql 备份数据
- android am start activity,adb shell am start -n package/.XXXactivity命令学习汇总
- k3修改服务器,金蝶k3客户端修改服务器地址
- 小米路由器mini无线连接HP1010打印机
- wps表格l制作甘特图_十分钟学会制作Excel甘特图,工作进度一目了然!
- [WARNING]: Could not match supplied host pattern, ignoring: servers
- 基于单片机汽车防盗报警系统设计-单片机毕业课程设计
- Keras:使用预训练模型迁移学习单通道灰度图像
- 二手平台android界面,Android二手书交易app设计(2)启动图Activity
- 圣诞的荒诞小故事并记录互联网协议-五层模型
- 影响计算机串口接收速度的因素,如何提高串口通信速度
- 2017HOTELEX Shanghai 3月28日闪亮登场
热门文章
- plugin.super mysql_使用MySQ Clone Plugin部署MySQL Group Replication
- php无法查询excel数据,laravel phpexcel无法读取excel中中文表头列数据
- docker教程_2 docker常见命令
- 小白重装系统教程_小白重装系统使用教程
- ES2018 学习笔记(4)Unicode 和 ISO 10646
- 网站排障的一些小命令
- Windows Mobile Sensors API库的设计
- Salesforce Ventures为云初创公司Vlocity注资5000万美元
- Redhat7.2下编译rpm包的形式安装openvswitch
- css的布局模型(三)—层模型