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

我輩豈是蓬蒿人!

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>
            国产精品女主播在线观看| 99视频精品全国免费| 久久久综合视频| 欧美在线www| 久久久91精品| 蜜桃av综合| 欧美日韩精品国产| 国产精品久久久一区二区三区| 国产精品国内视频| 国模私拍视频一区| 91久久精品日日躁夜夜躁国产| 亚洲精品三级| 亚洲欧美国产精品桃花| 欧美一区二区三区四区在线观看地址| 久久精品视频在线播放| 欧美国产在线电影| 一区二区高清视频| 久久爱www| 欧美777四色影视在线| 欧美日韩一区精品| 国精产品99永久一区一区| av成人国产| 久久综合国产精品| 日韩性生活视频| 欧美一区二区三区视频| 麻豆精品视频在线观看视频| 欧美日韩精品免费观看视频完整 | 久久激情久久| 欧美第一黄网免费网站| 国产精品成人一区二区网站软件| 国产精品色网| 亚洲精品一区二区三区在线观看| 欧美日本亚洲视频| 国产乱肥老妇国产一区二 | 免费欧美日韩| 国产精品成人免费| 亚洲黄色高清| 亚洲一区区二区| 欧美国产精品| 久久av红桃一区二区小说| 欧美激情国产日韩精品一区18| 国产午夜精品麻豆| 一区二区日韩精品| 免费日韩成人| 久久精品九九| 国产欧美日韩一区二区三区在线| 亚洲精品网站在线播放gif| 久久久精品tv| 亚洲与欧洲av电影| 欧美日韩不卡合集视频| 亚洲高清久久网| 久久久久九九九九| 亚洲欧美日韩一区二区在线| 欧美日韩大片| 一区二区日韩| 亚洲精选在线观看| 欧美另类视频| 99这里有精品| 亚洲精品一区二区三区樱花| 欧美二区乱c少妇| 在线日韩av片| 欧美高清不卡| 久久只精品国产| 在线日韩视频| 欧美激情精品| 欧美伦理a级免费电影| 亚洲激情第一页| 欧美韩国日本一区| 欧美成人高清视频| 亚洲国产欧美另类丝袜| 男女视频一区二区| 欧美h视频在线| 999亚洲国产精| 在线午夜精品| 国产亚洲福利| 蜜桃av噜噜一区| 久久这里只有精品视频首页| 亚洲精选久久| 亚洲一品av免费观看| 国产亚洲毛片| 欧美成人免费在线观看| 欧美精品成人一区二区在线观看| 日韩手机在线导航| 在线亚洲伦理| 国产真实乱偷精品视频免| 老司机aⅴ在线精品导航| 欧美超级免费视 在线| 亚洲美女在线国产| 亚洲在线视频| 亚洲高清一二三区| 99精品免费视频| 国产午夜久久| 亚洲精品日韩久久| 亚洲视频精品| 影音先锋在线一区| 91久久精品国产91性色tv| 免费人成网站在线观看欧美高清| 艳女tv在线观看国产一区| 亚洲小说欧美另类婷婷| 韩国一区二区三区美女美女秀| 亚洲电影一级黄| 国产精品99免费看| 欧美激情视频免费观看| 国产精品成人一区二区三区吃奶 | 在线亚洲欧美| 亚洲福利国产| 午夜精品一区二区三区在线播放| 亚洲大胆女人| 性做久久久久久| 在线一区二区视频| 久久综合九色欧美综合狠狠| 欧美一级日韩一级| 欧美日韩高清不卡| 蜜桃av噜噜一区| 国产视频精品网| 99精品免费| 99国产精品一区| 久久久久久一区| 久久久91精品| 国产伦精品一区二区三区在线观看| 最新国产乱人伦偷精品免费网站| 精品成人在线| 久久国内精品自在自线400部| 亚洲午夜久久久| 欧美成人免费全部| 欧美成人四级电影| 黄色精品免费| 欧美中文在线字幕| 久久精品国产99国产精品| 欧美视频精品一区| 91久久精品国产91久久| 在线欧美日韩国产| 久久久久久亚洲综合影院红桃| 欧美一区网站| 国产九九精品| 性欧美8khd高清极品| 新狼窝色av性久久久久久| 欧美天天在线| 在线综合欧美| 午夜天堂精品久久久久| 国产精品九色蝌蚪自拍| 一区二区三区日韩精品视频| 夜夜嗨av一区二区三区四区 | 欧美99久久| 亚洲国产精品一区制服丝袜| 欧美专区在线| 欧美成人中文| 99pao成人国产永久免费视频| 欧美大学生性色视频| 欧美国产三级| 一本色道久久综合亚洲精品小说| 欧美日韩久久久久久| 夜夜嗨网站十八久久| 中文亚洲视频在线| 欧美日韩在线视频一区二区| 一区二区三区日韩欧美精品| 国产精品av久久久久久麻豆网| 一区二区免费看| 欧美一区三区三区高中清蜜桃| 好看的日韩av电影| 女同一区二区| 一区二区三区精品视频在线观看| 亚洲在线视频一区| 国内不卡一区二区三区| 欧美jizzhd精品欧美喷水| 亚洲精品在线二区| 久久精品最新地址| 亚洲精品欧美激情| 国产精品久久久久av| 久久久久国色av免费看影院| 欧美高清自拍一区| 欧美伊久线香蕉线新在线| 在线观看国产欧美| 欧美三级视频在线| 久久久久免费| 亚洲视频图片小说| 美女精品国产| 亚洲一区二区3| 激情综合色综合久久| 欧美精品一区二区三区四区| 香蕉久久夜色精品国产使用方法| 欧美高清视频在线观看| 亚洲欧美国内爽妇网| 一色屋精品视频免费看| 欧美日韩精品在线| 麻豆精品网站| 欧美一区在线看| 亚洲理伦在线| 欧美国产国产综合| 久久久久网站| 亚洲欧美清纯在线制服| 亚洲精品一区二区三区蜜桃久| 韩国一区电影| 国产农村妇女精品一二区| 欧美人与性禽动交情品 | 国产精品爱久久久久久久| 久久中文字幕一区| 欧美在线观看网址综合| 亚洲天堂av高清| 99精品国产热久久91蜜凸|