青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我輩豈是蓬蒿人!

C++ && keyWordSpotting

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  11 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

常用鏈接

留言簿(9)

我參與的團隊

搜索

  •  

積分與排名

  • 積分 - 7323
  • 排名 - 1369

最新評論

閱讀排行榜

評論排行榜

(一)

One of the basic classes implemented by the Standard Template Library is the vector class. A vector is, essentially, a resizeable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory. Fortunately, the memory requirements are equivalent to those of a normal array. The header file for the STL vector library is vector. (Note that when using C++, header files drop the .h; for C header files - e.g. stdlib.h - you should still include the .h.) Moreover, the vector class is part of the std namespace, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program.

Vectors are more powerful than arrays because the number of functions that are available for accessing and modifying vectors. Unfortunately, the [] operator still does not provide bounds checking. There is an alternative way of accessing the vector, using the function at, which does provide bounds checking at an additional cost. Let's take a look at several functions provided by the vector class:

1?unsigned?int?size();?//?Returns?the?number?of?elements?in?a?vector
2?push_back(type?element);?//?Adds?an?element?to?the?end?of?a?vector
3?bool?empty();?//?Returns?true?if?the?vector?is?empty
4?void?clear();?//?Erases?all?elements?of?the?vector
5?type?at(int?n);?//Returns?the?element?at?index?n,?with?bounds?checking

also, there are several basic operators defined for the vector class:

=           Assignment replaces a vector's contents with the contents of another
==          An element by element comparison of two vectors
[]          Random access to an element of a vector (usage is similar to that
            of the operator with arrays.) Keep in mind that it does not provide
            bounds checking.

Let's take a look at an example program using the vector class:

				
?1?#include?<iostream>
?2?#include?<vector>
?3?using?namespace?std;
?4?
?5?int?main()
?6?{
?7?????vector?<int>?example;?????????//Vector?to?store?integers
?8?????example.push_back(3);?????????//Add?3? onto?the?vector
?9?????example.push_back(10);????????//Add?10?to?the?end
10?????example.push_back(33);????????//Add?33?to?the?end

11?????for(int?x=0;?x<example.size();?x++)?
12?????{
13?????????cout<<example[x]<<"?";????//Should?output:?3?10?33
14?????}

15?????if(!example.empty())??????????//Checks?if?empty
16?????????example.clear();??????????//Clears?vector
17?????vector?<int>?another_vector;??//Creates?another?vector?to?store?integers
18?????another_vector.push_back(10);?//Adds?to?end?of?vector
19?????example.push_back(10);????????//Same
20?????if(example==another_vector)???//To?show?testing?equality
21?????{
22?????????example.push_back(20);?
23?????}
24?????for(int?y=0;?y<example.size();?y++)
25?????{
26?????????cout<<example[y]<<"?";????//Should?output?10?20
27?????}
28?????return?0;
29?}
Summary of Vector Benefits

Vectors are somewhat easier to use than regular arrays. At the very least, they get around having to be resized constantly using new and delete. Furthermore, their immense flexibility - support for any datatype and support for automatic resizing when adding elements - and the other helpful included functions give them clear advantages to arrays.

Another argument for using vectors are that they help avoid memory leaks--you don't have to remember to free vectors, or worry about how to handle freeing a vector in the case of an exception. This simplifies program flow and helps you write tighter code. Finally, if you use the at() function to access the vector, you get bounds checking at the cost of a slight performance penalty.

(二)

?C++ vector is a container template available with Standard Template Library pack. This C++ vector can be used to store similar typed objects sequentially, which can be accessed at a later point of time. As this is a template, all types of data including user defined data types like struct and class can be stored in this container.

This article explains briefly about how to insert, delete, access the data with respect to the C++ vector. The type stl::string is used for the purposes of explaining the sample code. Using stl::string is only for sample purposes. Any other type can be used with the C++ vector.

The values can be added to the c++ vector at the end of the sequence. The function push_back should be used for this purpose. The <vector> header file should be included in all the header files in order to access the C++ vector and its functions.

?1?#include?<vector>
?2?#include?<string>
?3?#include?<iostream.h>
?4?
?5?void?main()
?6?{
?7?????//Declaration?for?the?string?data
?8?????std::string?strData?=?"One";
?9?????//Declaration?for?C++?vector
10?????std::?vector?<std::string>?str_Vector;
11?????str_Vector.push_back(strData);
12?????strData?=?"Two";
13?????str_Vector.push_back(strData);
14?????strData?=?"Three";
15?????str_Vector.push_back(strData);
16?????strData?=?"Four";
17?????str_Vector.push_back(strData);
18?}

The above code adds 4 strings of std::string type to the str_Vector. This uses std:: vector.push_back function. This function takes 1 parameter of the designated type and adds it to the end of the c++ vector.

Accessing Elements of C++ Vector:

?? The elements after being added can be accessed in two ways. One way is our legacy way of accessing the data with vector.at(position_in_integer) function. The position of the data element is passed as the single parameter to access the data.

Using our normal logic for accessing elements stored in C++ Vector:

If we want to access all the data, we can use a for loop and display them as follows.

1?for(int?i=0;i?<?str_Vector.size();?i++)
2?{
3?????std::string?strd?=?str_Vector.at(i);
4?????cout<<strd.c_str()<<endl;
5?}

The std:: vector .size() function returns the number of elements stored in the vector.

Using C++ vector iterator provided by STL:

The next way is to use the iterator object provided by the STL. These iterators are general purpose pointers allowing c++ programmers to forget the intricacies of object types and access the data of any type.

1?std::vector<std::string>::iterator?itVectorData;
2?for(itVectorData?=?str_Vector.begin();??itVectorData?!=?str_Vector.end();?itVectorData++)
3?{
4?????std::string?strD?=?*(itVectorData);
5?}

Removing elements from C++ vector:

Removing elements one by one from specified positions in c++ vector is achieved with the erase function.

The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.

1 str_Vector.erase(str_Vector.begin() + 1 ,str_Vector.begin() + 2 );

?The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.?

1 str_Vector.erase(str_Vector.begin() + 1 ,str_Vector.begin() + 3 );

If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.

posted on 2006-08-13 19:36 keyws 閱讀(817) 評論(0)  編輯 收藏 引用 所屬分類: STL

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲伊人伊色伊影伊综合网| 日韩亚洲欧美一区二区三区| 久久综合中文色婷婷| 午夜久久久久| 久久国产一区| 免费不卡在线视频| 免费欧美视频| 欧美色图天堂网| 国产区精品在线观看| 国内精品视频666| 亚洲国产精品视频| 在线一区日本视频| 久久国产精品第一页| 欧美成年人视频网站欧美| 欧美激情一区在线观看| 亚洲伦伦在线| 欧美一区二区三区在线| 欧美成人精品三级在线观看| 国产精品久久久久9999高清| 在线日韩一区二区| 亚洲一区二区三区乱码aⅴ蜜桃女| 久久国产精品72免费观看| 亚洲高清视频在线| 亚洲人成在线观看一区二区| 欧美一区二区三区四区在线观看地址 | 中日韩男男gay无套| 亚洲欧美制服中文字幕| 久久伊人免费视频| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ入口 | 久久久福利视频| 老司机午夜精品| 久久xxxx精品视频| 久久久www成人免费毛片麻豆| 美女在线一区二区| 国产精品视频免费观看www| 欲香欲色天天天综合和网| 亚洲天堂成人在线视频| 裸体女人亚洲精品一区| 在线视频欧美精品| 欧美国产日本| 永久91嫩草亚洲精品人人| 亚洲欧美日产图| 亚洲国产一区二区精品专区| 久久av一区二区三区亚洲| 欧美日韩播放| 亚洲三级国产| 模特精品裸拍一区| 亚洲欧美一区二区精品久久久| 欧美精品一区二区三区视频| 在线观看成人av电影| 久久久不卡网国产精品一区| 亚洲与欧洲av电影| 国产精品久久久999| 99综合视频| 亚洲国产欧美日韩| 欧美大秀在线观看| 亚洲欧洲一区二区三区久久| 欧美极品在线视频| 国产日韩在线看片| 亚洲欧美日韩精品一区二区| 亚洲毛片av在线| 欧美久久影院| 亚洲深夜av| 亚洲无线视频| 国产女人精品视频| 久久国产精品电影| 久久久免费av| 亚洲黄色三级| 亚洲免费大片| 国产精品视频一区二区三区| 欧美一激情一区二区三区| 亚洲欧美不卡| 国产性猛交xxxx免费看久久| 久久精品在线免费观看| 欧美伊人久久| 亚洲福利在线视频| 亚洲成色999久久网站| 免费欧美日韩国产三级电影| 99在线精品视频在线观看| 亚洲作爱视频| 国产模特精品视频久久久久| 久久久999精品| 久久综合九九| 这里只有精品电影| 亚洲一区二区三区精品视频| 国产自产女人91一区在线观看| 蜜桃av噜噜一区| 欧美久久九九| 久久精品视频在线播放| 美女免费视频一区| 亚洲欧美日韩综合国产aⅴ| 欧美与黑人午夜性猛交久久久| 久久不射网站| 狼人社综合社区| 亚洲图片在线观看| 欧美在线短视频| 亚洲日韩欧美视频| 亚洲女ⅴideoshd黑人| 一区二区在线观看视频| 最新中文字幕一区二区三区| 国产欧美一区二区三区沐欲| 亚洲第一页中文字幕| 国产精品一级二级三级| 欧美国产日韩在线| 国产精品网站在线播放| 欧美韩日一区二区| 国产精品综合| 亚洲国产欧美在线人成| 国产日韩精品入口| 亚洲精品视频免费在线观看| 国产日韩欧美日韩| 亚洲精品视频一区| 亚洲第一毛片| 久久成人国产精品| 午夜久久一区| 欧美另类综合| 欧美阿v一级看视频| 国产精品自拍在线| 一本一本久久| 亚洲美女在线观看| 久久九九热re6这里有精品| 亚洲在线免费观看| 蜜臀av性久久久久蜜臀aⅴ| 久久精品人人爽| 国产精品久久97| 日韩手机在线导航| 亚洲高清免费视频| 欧美在线三级| 久久国产精品99久久久久久老狼 | 老司机一区二区| 国产乱码精品一区二区三区av| 亚洲伦伦在线| 亚洲系列中文字幕| 欧美日韩精品三区| 99re这里只有精品6| 亚洲理论在线| 欧美成人一区在线| 亚洲黑丝在线| 99re6这里只有精品视频在线观看| 久久综合亚洲社区| 欧美激情精品久久久久久免费印度 | 国产精品成人观看视频免费| av不卡免费看| 午夜精品久久久| 国产视频综合在线| 久久国内精品视频| 麻豆成人综合网| 亚洲福利在线看| 欧美久久久久免费| 制服诱惑一区二区| 久久成人人人人精品欧| 国产一区二区成人| 久久精品30| 欧美韩国在线| 日韩视频亚洲视频| 亚洲欧美日韩电影| 国产亚洲欧美日韩一区二区| 久久另类ts人妖一区二区| 免费国产自线拍一欧美视频| 亚洲国产日韩在线| 欧美日韩国产成人| 亚洲自拍偷拍视频| 久久亚裔精品欧美| 亚洲经典在线| 国产精品久久午夜夜伦鲁鲁| 欧美在线播放高清精品| 欧美激情第9页| 亚洲欧美国产精品专区久久| 国产一区二区久久精品| 你懂的视频一区二区| 中日韩视频在线观看| 蜜臀av国产精品久久久久| 9久re热视频在线精品| 国产欧美日韩视频在线观看| 蜜桃av一区二区三区| 国产精品99久久久久久白浆小说| 久久国产精品久久久久久| 亚洲国产精品嫩草影院| 欧美三级精品| 免费成人网www| 亚洲欧美成人网| 91久久精品国产| 久久裸体视频| 亚洲永久免费精品| 亚洲成在线观看| 国产日韩精品一区二区三区在线| 欧美成人精品三级在线观看 | 亚洲日本精品国产第一区| 午夜国产一区| 亚洲免费观看在线观看| 国产亚洲欧美另类一区二区三区| 欧美精品一区在线发布| 久久久久国产精品一区| 亚洲免费视频成人| 日韩午夜电影av| 亚洲国产精品va在线看黑人| 久久久99国产精品免费| 欧美一级视频| 亚洲欧美一区二区视频| 在线综合亚洲|