Zi 字媒體
2017-07-25T20:27:27+00:00
C++ STL 容器基本範例實驗測試[02_CPP_STL_Vector ->純C++ Vector範例]
資料來源:https://www.cnblogs.com/skyfsm/p/6934246.html
http://larry850806.github.io/2016/06/06/STL1/
http://larry850806.github.io/2016/06/06/STL2/
GITHUB 完整資料: https://github.com/jash-git/Base_CPP_STL_Example
Vector 可以看成是一個動態陣列
用法跟陣列很像,基本功能有 :
▲ push_back: 把一個值加到尾巴
▲ pop_back: 把尾巴的值移除掉
▲ size: 得到目前長度(元素個數)
▲ []: 得到某一個位置的值
▲ clear: 把元素清空
Vector 的優點
▲ 宣告時可以不用確定大小
▲ 可以 Random Access
Vector 的缺點
▲ 在內部進行刪除時效率很低
本範例額外紀錄
▲ 純C數值(整數/浮點數)轉字串
▲ C++字串相加
#include
#include
#include /* malloc, free, rand */
#include /*strncpy .....*/
#include /*c++ string class*/
#include
using namespace std;
/*
Vector 可以看成是一個動態陣列
用法跟陣列很像,基本功能有 :
▲ push_back: 把一個值加到尾巴
▲ pop_back: 把尾巴的值移除掉
▲ size: 得到目前長度(元素個數)
▲ []: 得到某一個位置的值
▲ clear: 把元素清空
Vector 的優點
▲ 宣告時可以不用確定大小
▲ 可以 Random Access
Vector 的缺點
▲ 在內部進行刪除時效率很低
本範例額外紀錄
▲ 純C數值(整數/浮點數)轉字串
▲ C++字串相加
*/
void Pause()
{
printf("Press Enter key to continue...");
fgetc(stdin);
}
int main()
{
vector vec; // 宣告一個裝 int 的 vector
// 現在 vec 是空的
vec.push_back(10);
vec.push_back(20); // 經過三次 push_back
vec.push_back(30); // vec 是 [10, 20, 30]
int length = vec.size(); // length = 3
for(int i=0 ; i vecStr;
string strbase = "value = ";
for(int i=0 ; i<5 ; i++){
strbase = "value = ";
char buf[30];
sprintf(buf,"%d",(i * 10));//純C數值(整數/浮點數)轉字串
strbase = strbase + buf;//C++字串相加
vecStr.push_back(strbase); // [0, 10, 20, 30, 40]
}
for(int i=0 ; i
寫了
5860316篇文章,獲得
23313次喜歡