使用vector.insert將array轉vector,雖然也是一行完成,但不是那麼直觀,建議還是用constructor的方式將array轉std::vector。
1/**//*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : ArrayToVectorByInsert.cpp
5Compiler : Visual C++ 8.0
6Description : Demo how to convert array to vector by vector.insert
7Release : 11/15/2006 1.0
8 12/10/2006 2.0
9*/
10#include <iostream>
11#include <vector>
12#include <algorithm>
13
14using namespace std;
15
16int main() {
17 int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
18
19 vector<int> ivec;
20 // We specify the address of the first element of
21 // the array as 2nd argument, and the address of
22 // one past the last element as 3rd argument.
23 ivec.insert(ivec.end(), ia, ia + sizeof(ia)/sizeof(int));
24 copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, "\n"));
25
26 return 0;
27}
See Also
(原創) 如何將array轉成std::vector? (使用constructor) (C/C++) (STL)