• <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>

            我輩豈是蓬蒿人!

            C++ && keyWordSpotting

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

            常用鏈接

            留言簿(9)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 7079
            • 排名 - 1377

            最新評論

            閱讀排行榜

            評論排行榜

            (一)

            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 閱讀(804) 評論(0)  編輯 收藏 引用 所屬分類: STL
            麻豆av久久av盛宴av| 精品国产乱码久久久久久1区2区 | 97精品伊人久久大香线蕉app| 久久综合精品国产二区无码| 99久久中文字幕| 久久亚洲2019中文字幕| 婷婷伊人久久大香线蕉AV| 亚洲精品高清久久| 色88久久久久高潮综合影院| 精品久久久久中文字| 韩国免费A级毛片久久| 久久综合亚洲色HEZYO国产| 久久天天躁狠狠躁夜夜96流白浆| 国产精品内射久久久久欢欢| 日产精品久久久久久久| 亚洲国产日韩综合久久精品| 99久久精品免费| 久久99精品久久久久久| 亚洲精品白浆高清久久久久久| 欧美激情精品久久久久久久九九九| 精品久久8x国产免费观看| 欧美亚洲国产精品久久| 久久亚洲欧洲国产综合| 国产精品综合久久第一页| 国产精品视频久久| 国内精品人妻无码久久久影院 | 一本一道久久综合狠狠老| 久久精品国产精品亚洲精品| 亚洲中文字幕无码一久久区| 亚洲性久久久影院| 伊人色综合久久天天人守人婷| 久久精品无码一区二区三区日韩| 久久久久久狠狠丁香| 久久精品国产99国产精品澳门| 亚洲欧美成人综合久久久| 无码久久精品国产亚洲Av影片| 亚洲中文字幕久久精品无码喷水 | 狠狠色丁香久久婷婷综合_中 | 精品久久久无码人妻中文字幕豆芽 | 久久精品视频一| 思思久久好好热精品国产|