[cpp]view plain
copy
print?
// erase功能:移除vector中部分的元素
void
test_vector_erase()
;
std::vector> vect(t, t + 7);
std::cout << "vect = "
; std::copy(vect.begin(), vect.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
// 過載方法1
vect.erase(vect.begin() + 2, vect.begin() + 5);
std::cout << "vect = "
; std::copy(vect.begin(), vect.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
// 重新初始化
vect.clear();
vect.push_back(10);
vect.push_back(11);
vect.push_back(12);
vect.push_back(13);
vect.push_back(14);
vect.push_back(15);
vect.push_back(16);
// 過載方法2
vect.erase(vect.begin() + 2);
std::cout << "vect = "
; std::copy(vect.begin(), vect.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
} // clean功能:移除vector中所有元素
void
test_vector_clear()
;
std::vector> vect(t, t + 7);
std::cout << "vect = "
; std::copy(vect.begin(), vect.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
vect.clear();
assert(vect.empty());
std::cout << "vect = "
; std::copy(vect.begin(), vect.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
} void
test_vector_assign()
;
std::vector> v1(t, t + 7);
std::vector> v2, v3;
v2.assign(v1.begin(), v1.end());
std::cout << "v2 = "
; std::copy(v2.begin(), v2.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
v3.assign(7, 10);
std::cout << "v3 = "
; std::copy(v3.begin(), v3.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
} void
test_vector_at()
;
std::vector> vect(t, t + 7);
int&i = vect.at(0);
int&j = vect.at(3);
int&k = vect.front();
int&m = vect.back();
assert(i == 10);
assert(j == 13);
assert(k == 10);
assert(m == 16);
} void
test_vector_pop()
;
std::vector> v1(t, t + 7);
v1.pop_back();
std::cout << "v1 = "
; std::copy(v1.begin(), v1.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
} void
test_vector_swap()
;
std::vector> v1(t, t + 7);
std::vector> v2(7, 10);
std::cout << "v1 = "
; std::copy(v1.begin(), v1.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
v2.swap(v1);
std::cout << "v1 = "
; std::copy(v1.begin(), v1.end(), std::ostream_iterator>(std::cout,
" "));
std::cout << std::endl;
} // iterator,const_iterator作用:遍歷容器內的元素,並訪問這些元素的值。
// 區別:iterator可以改元素值,但const_iterator不可改。
void
test_vector_const_iterator()
;
std::vector> v1(t, t + 7);
std::cout << "v1 = "
;
for(std::vector<
int>::const_iterator cit = v1.begin(); cit != v1.end(); ++cit)
std::cout << *cit << " "
; std::cout << std::endl;
} // 功能:為vector預分配記憶體空間。
void
test_vector_reserve()
Vector常用函式
菜鳥教程vector詳細介紹 1.概念 向量 vector 是乙個封裝了動態大小陣列的順序容器 sequence container 跟任意其它型別容器一樣,它能夠存放各種型別的物件。可以簡單的認為,向量是乙個能夠存放任意型別的動態陣列。2.初始化 vector int vec 宣告乙個int型向量...
vector的常用建構函式
vector常用建構函式 建構函式 作用 vector 建立乙個沒有任何元素的空向量 vector vector right 建立乙個向量,並使用向量 right初始化該向量 vector size type count 建立乙個大小為count的向量vect vector size type co...
容器vector的常用介面函式
vectorv1 vector iterator iter v1.push back 10.4 v1.push back 20.2 v1.push back 30.7 for iter v1.begin iter v1.end iter cout iter 輸出10.4 20.2 30.7 v1.p...