C++容器類對象函數參數問題,容器函數

來源:互聯網
上載者:User

C++容器類對象函數參數問題,容器函數

總之一句話:容器類對象作為函數參數,與整數類型作為函數參數的傳遞特性相同。

驗證程式

#include "stdafx.h"#include <iostream>#include <list>using namespace std;void do_something1(list<int> *ls){ls->push_back(1);ls->push_back(2);ls->push_back(3);return;}void do_something2(list<int> ls){ls.push_back(1);ls.push_back(2);ls.push_back(3);return;}void do_something3(list<int> &ls){ls.push_back(1);ls.push_back(2);ls.push_back(3);return;}int main(void){list<int> ls1,ls2,ls3;cout<<"**************"<<endl;cout<<"ls1:"<<endl;cout<<ls1.size()<<endl;do_something1(&ls1);cout<<ls1.size()<<endl;cout<<"**************"<<endl;cout<<"ls2:"<<endl;cout<<ls2.size()<<endl;do_something2(ls2);cout<<ls2.size()<<endl;cout<<"**************"<<endl;cout<<"ls3:"<<endl;cout<<ls3.size()<<endl;do_something3(ls3);cout<<ls3.size()<<endl;system("pause");return 0;}



編一個模版函數 以容器物件為參數 輸出容器物件中的所有元素

//The caller need release pOutArray

template<class T>
void Terminate(vector<T>& vec, T* pOutArray)
{
int size = vec.size();

pOutArray = new T[size];

for (int i=0; i < size; i++)
{
*pOutArray[i] = vec[i];
}
}
 
c++容器,成員函數Insert傳遞3個迭代器參數的使用問題

#include <vector>
vector屬於std命名域的,因此需要通過命名限定,如下完成你的代碼:
using std::vector;
vector<int> vInts;
或者連在一起,使用全名:
std::vector<int> vInts;
建議使用全域的命名域方式:using namespace std;
函數
表述
c.assign(beg,end)c.assign(n,elem)
將[beg; end)區間中的資料賦值給c。將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)
c.erase(beg,end)
刪除pos位置的資料,傳回下一個資料的位置。
刪除[beg,end)區間的資料,傳回下一個資料的位置。
c.front()
傳回第一個資料。
get_allocator
使用建構函式返回一個拷貝。
c.insert(pos,elem)
c.insert(pos,n,elem)
c.insert(pos,beg,end)
在pos位置插入一個elem拷貝,傳回新資料位元置。在pos位置插入n個elem資料。無傳回值。在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)
swap(c1,c2)
將c1和c2元素互換。同上操作。
vector<Elem>
cvector<Elem> c1(c2)
vector <Elem> c(n)
ector <Elem> c(n, elem)
vector <Elem> c(beg,end)
c.~ vector <Elem>()
建立一個空的vector。複製一個vector。建立一個vector,含有n個資料,資料均已預設構造產生。建立一個含有n個elem拷貝的vector。建立一個以[beg;end)區間的vector。銷毀所有資料,釋放記憶體。
operator[]
返回容器中指定位置的一個引用。
建立一個vector
vector容器提供了多種建立方法,下面介紹幾種常用的。
建立一個Widget類型的空的vector對象:
vector<Widget> vWidgets;
......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.