(原創) 如何将array转成std::vector? (使用vector.insert) (C/C++) (STL)
使用vector.insert將array轉vector,雖然也是一行完成,但不是那麼直觀,建議還是用constructor的方式將array轉std::vector。
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ArrayToVectorByInsert.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to convert array to vector by vector.insert
7
Release : 11/15/2006 1.0
8
12/10/2006 2.0
9
*/
10
#include <iostream>
11
#include <vector>
12
#include <algorithm>
13
14
using namespace std;
15
16
int 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
}
/* 2
(C) OOMusou 2006 http://oomusou.cnblogs.com3

4
Filename : ArrayToVectorByInsert.cpp5
Compiler : Visual C++ 8.06
Description : Demo how to convert array to vector by vector.insert7
Release : 11/15/2006 1.08
12/10/2006 2.09
*/10
#include <iostream>11
#include <vector>12
#include <algorithm>13

14
using namespace std;15

16
int 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)


浙公网安备 33010602011771号