C++基础知识(七) vector使用方法,at()用法

Vector成员函数

函数表述
c.assign(beg,end)将[beg; end)区间中的数据赋值给c。
c.assign(n,elem)将n个elem的拷贝赋值给c。
c.at(idx)传回索引idx所指的数据,如果idx越界,抛出out_of_range。
c.back()传回最后一个数据,不检查这个数据是否存在。
c.begin()传回迭代器重的可一个数据。
c.capacity()返回容器中数据个数。
c.clear()移除容器中所有数据。
c.empty()判断容器是否为空。
c.end()指向迭代器中的最后一个数据地址。
c.erase(pos)删除pos位置的数据,传回下一个数据的位置。
c.erase(beg,end)删除[beg,end)区间的数据,传回下一个数据的位置。
c.front()传回第一个数据。
get_allocator使用构造函数返回一个拷贝。
c.insert(pos,elem)在pos位置插入一个elem拷贝,传回新数据位置。
c.insert(pos,n,elem)在pos位置插入n个elem数据。无返回值。
c.insert(pos,beg,end)在pos位置插入在[beg,end)区间的数据。无返回值
c.max_size()返回容器中最大数据的数量
c.pop_back()删除最后一个数据。
c.push_back(elem)在尾部加入一个数据。
c.rbegin()传回一个逆向队列的第一个数据。
c.rend()传回一个逆向队列的最后一个数据的下一个位置。
c.resize(num)重新指定队列的长度。
c.reserve()保留适当的容量。
c.size()返回容器中实际数据的个数。
c1.swap(c2)将c1和c2元素互换。
swap(c1,c2)同上操作。

访问vector中的数据
使用两种方法来访问vector。

1、 vector::at()

2、 vector::operator[]
operator[]主要是为了与C语言进行兼容。它可以像C语言数组一样操作。但at()是我们的首选,因为at()进行了边界检查,如果访问超过了vector的范围,将抛出一个例外。由于operator[]容易造成一些错误,所有我们很少用它,下面进行验证一下:

分析下面的代码:

vector<int> v;
v.reserve(10);
 
for(int i=0; i<7; i++)
    v.push_back(i);
 
try
{
    int iVal1 = v[7];  // not bounds checked - will not throw
    int iVal2 = v.at(7); // bounds checked - will throw if out of range
}
 
catch(const exception& e)
{
    cout << e.what();
}

我们使用reserve()分配了10个int型的空间,但并不没有初始化。

你可以在这个代码中尝试不同条件,观察它的结果,但是无论何时使用at(),都是正确的。

  • 4
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
==================================================================== = Contents of this CD-ROM: = ==================================================================== README.TXT - this file \CANscope\ CANscope installation version. ==================================================================== = Quick Install: = ==================================================================== INSTALL APPLICATION: Press [Install application] at the autorun feature. If this dosen't work, follow these steps: 1. Run the program \CANscope\SETUP.EXE. 2. Follow the instructions to complete the software installation. ==================================================================== = DISCLAIMER = = = = The text and software on this CD ROM = = were prepared with great care and diligence. However, = = Vector Informatik GmbH cannot assume legal = = responsibility or liability for any residual errors = = or their consequences. = = All of the software, hardware and trademark names used = = on this CD ROM are subject to general copyright = = protection of trade names, trade marks and patent = = rights. = ==================================================================== (c) 10/2008 Vector Informatik GmbH ==================================================================== = Addresses: = ==================================================================== Headquarter Germany: -------------------- Vector Informatik GmbH Ingersheimer Stra遝 24 D-70499 Stuttgart Phone: +49 711 80670-0 Fax: +49 711 80670-111 E-mail: can@vector-informatik.de Internet: http://www.vector-informatik.de Subsidiary USA: --------------- Vector CANtech, Inc. Suite 550 39500 Orchard Hill Place USA-Novi, Mi 48375 Phone: +1 248 449-9290 Fax: +1 248 449-9704 E-mail: info@vector-cantech.com Internet: http://www.vector-cantech.com Subsidiary Japan: ----------------- Vector Japan Co., Ltd. Sea Fort Square Center Build. 18F 2-3-12 Higashi-shinagawa, Shinagawa-ku J-140-0002 Tokyo Phone: +81 3 5769 6970 Fax: +81 3 5769 6975 E-mail: info@vector-japan.co.jp Internet: http://www.vector-japan.co.jp Subsidiary France: ------------------ Vector France 168, Boulevard Cam閘iant F-92240 Malakoff Phone: +33 1 4231 4000 Fax: +33 1 4231 4009 E-mail: info@vector-france.com Internet: http://www.vector-france.com Subsidiary Sweden: ------------------ VecScan AB Theres Svenssons Gata 9 417 55 G鰐eborg Phone: +46 (0)31 764 76 00 Fax: +46 (0)31 764 76 19 E-mail: [email protected] Internet: www.vector-scandinavia.com Subsidiary Korea: ----------------- Vector Korea IT Inc. Daerung Post Tower III, 508 182-4 Guro-dong, Guro-gu Seoul 152-790 REPUBLIC OF KOREA Phone: +82(0)2 2028 0600 Fax: +82(0)2 2028 0604 E-mail: info@vector-korea.com Internet: www.vector-korea.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值