|
建議使用全局的命名域方式:usingnamespace 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> c
vector<Elem> c1(c2)
vector <Elem> c(n)
vector <Elem> c(n, elem)
vector <Elem> c(beg,end)
c.~ vector <Elem>()
|
創建一個空的vector。
複製一個vector。
創建一個vector,含有n個數據,數據均已缺省構造產生。
創建一個含有n個elem拷貝的vector。
創建一個以[beg;end)區間的vector。
銷燬所有數據,釋放內存。
|
||||||
|
operator[]
|
返回容器中指定位置的一個引用。
|
|
|
|||
|
vector<Widget> vWidgets;
|
vector<Widget> vWidgets(500);
|
vector<Widget> vWidgets(500, Widget(0));
|
vector<Widget> vWidgetsFromAnother(vWidgets);
|
for(int i= 0;i<10; i++)
vWidgets.push_back(Widget(i));
|
int nSize = v.empty() ? -1 :
static_cast<int>(v.size());
|
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();
}
|
#include <algorithm>
|
#include <functional>
enum findmodes
{
FM_INVALID = 0,
FM_IS,
FM_STARTSWITH,
FM_ENDSWITH,
FM_CONTAINS
};
typedefstruct tagFindStr
{
UINT iMode;
CString szMatchStr;
} FindStr;
typedef FindStr* LPFINDSTR;
|
class FindMatchingString
: public std::unary_function<CString,
bool>
{
public:
FindMatchingString(const LPFINDSTR lpFS) : m_lpFS(lpFS) {}
bool operator()(CString& szStringToCompare)
const
{
bool retVal =
false;
switch(m_lpFS->iMode)
{
case FM_IS:
{
retVal = (szStringToCompare == m_lpFDD->szMatchStr);
break;
}
case FM_STARTSWITH:
{
retVal = (szStringToCompare.Left(m_lpFDD->szMatchStr.GetLength())
== m_lpFDD->szWindowTitle);
break;
}
case FM_ENDSWITH:
{
retVal = (szStringToCompare.Right(m_lpFDD->szMatchStr.GetLength())
== m_lpFDD->szMatchStr);
break;
}
case FM_CONTAINS:
{
retVal = (szStringToCompare.Find(m_lpFDD->szMatchStr) != -1);
break;
}
}
return retVal;
}
private:
LPFINDSTR m_lpFS;
};
|
FindStr fs;
fs.iMode = FM_CONTAINS;
fs.szMatchStr = szRemove;
vs.erase(std::remove_if(vs.begin(), vs.end(), FindMatchingString(&fs)), vs.end());
|