Если вы используете sdk 23 или выше, вы должны проверить разрешения времени выполнения.
Попробуйте передать массив в вектор:
int arr[] = {2,5,8,11,14};
std::vector<int> TestVector(arr, arr+5);
Вы всегда можете вызвать std :: vector :: assign , чтобы назначить массив вектору, вызвать std :: vector :: insert для добавления нескольких массивов.
Если вы используете C ++ 11, вы можете попробовать:
std::vector<int> v{2,5,8,11,14};
Или
std::vector<int> v = {2,5,8,11,14};
Вы также можете использовать vector :: insert .
std::vector<int> v;
int a[5] = {2, 5, 8, 11, 14};
v.insert(v.end(), a, a+5);
Изменить:
Конечно, в реальном программировании вы должны использовать:
v.insert(v.end(), a, a+(sizeof(a)/sizeof(a[0]))); // C++03
v.insert(v.end(), std::begin(a), std::end(a)); // C++11
Вы можете сделать это с помощью списка инициализаторов:
std::vector<unsigned int> array;
// First argument is an iterator to the element BEFORE which you will insert:
// In this case, you will insert before the end() iterator, which means appending value
// at the end of the vector.
array.insert(array.end(), { 1, 2, 3, 4, 5, 6 });
с использованием vector :: insert (const_iterator position, initializer_list il); http://www.cplusplus.com/reference/vector/vector/insert/
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
vec.insert(vec.end(),{1,2,3,4});
return 0;
}
Вы также можете использовать Boost.Assignment :
const list<int> primes = list_of(2)(3)(5)(7)(11);
vector<int> v;
v += 1,2,3,4,5,6,7,8,9;