所以我尝试做一些像std::vector<int> WidthNumbers = 320, 640, 1280;
,但如果你的编译器支持的C++ 0x编译器给我的错误C2440: 'int' to 'std::vector<_Ty>'
如何修复std :: vector中的错误<int> WidthNumbers = 320,640,1280 ;?
1
A
回答
5
你不能使用该语法初始化一个vector
。的C++ 0x允许初始化列表,让你可以使用以下命令:
std::vector<int> WidthNumbers = {320, 640, 1280};
但是,这并没有在VS2010中实现。替代方案是:
int myArr[] = {320, 640, 1280};
std::vector<int> WidthNumbers(myArr, myArr + sizeof(myArr)/sizeof(myArr[0]));
OR
std::vector<int> WidthNumbers;
WidthNumbers.push_back(320);
WidthNumbers.push_back(640);
WidthNumbers.push_back(1280);
1
(MSVC++ 2010具有partial support for C++0x),你可以使用initializer list
std::vector<int> WidthNumbers = {320, 640, 1280};
+0
但是,VC++ 2010的部分支持不*包含此功能。 – 2012-04-29 14:15:29
2
您还可以使用boost::assign::list_of
#include <boost/assign/list_of.hpp>
#include <vector>
int
main()
{
typedef std::vector<int> WidthNumbers;
const WidthNumbers foo = boost::assign::list_of(320)(640)(1280);
}
0
这是稍微更多的工作,但我觉得它工作得很好VS 2010中。您可以使用_vector.push_back()
方法手动将项目添加到矢量,而不是使用初始化程序列表,如下所示:
//forward declarations
#include <vector>
#include <iostream>
using namespace std;
// main() function
int _tmain(int argc, _TCHAR *argv)
{
// declare vector
vector<int> _vector;
// fill vector with items
_vector.push_back(1088);
_vector.push_back(654);
_vector.push_back(101101);
_vector.push_back(123456789);
// iterate through the vector and print items to the console
vector<int>::iterator iter = _vector.begin();
while(iter != _vector.end())
{
cout << *iter << endl;
iter++;
}
// pause so you can read the output
system("PAUSE");
// end program
return 0;
}
这是我个人声明和初始化向量的方式,它总是对我的作品
相关问题
- 1. iterate std :: vector <std :: vector <char>>?
- 2. 修复使用Android NDK和std ::时发生的Eclipse错误:: vector
- 3. 错误`std :: vector <std :: unique_ptr < T >>`
- 4. 如何迭代boost :: variant <std :: vector <int>,std :: vector <String>>?
- 5. C++ std :: vector <std :: shared_ptr>
- 6. 错误2664 - 无法从std :: vector <...>转换为std :: tr1 :: shared_ptr
- 7. C++ std :: sort on std :: vector <Object> - 错误太多
- 8. std :: vector <T>
- 9. 从std :: istreambuf_iterator <>复制到std :: vector <>
- 10. 错误,而继承std :: vector
- 11. 错误与std :: vector push_back
- 12. 如何将std :: vector <unsigned short>转换为std :: vector <int>?
- 13. 如何在不复制的情况下更新std :: unordered_map <std :: string,std :: vector <int>>中的向量?
- 14. std :: sort on std :: vector <std::string>
- 15. 将std :: map <int,vector <int>>复制到std :: map <std:string,vector <int>>
- 16. 将std :: vector <std :: pair <const K, V> *>转换为std :: vector <std :: pair <const K, V>>
- 17. 将std :: vector <std :: unique_ptr <T>>移动到std :: vector <std :: shared_ptr <T>>
- 18. C++循环std :: vector <std :: map <std :: string,std :: string>>
- 19. C++ - 如何将std :: priority_queue中的元素复制到std :: vector
- 20. 如何将std :: vector <const char*>复制到void *?
- 21. 类型'std :: vector <Object*>&'&'&'
- 22. 错误:'operator *'不匹配(操作数类型为'std :: vector <std :: complex <double>>'和'std :: complex <double>')
- 23. std :: size_t或std :: vector <Foo> :: size_type?
- 24. 错误C2039:'vector':不是'std'的成员
- 25. 在std :: vector上使用std :: unique()<std :: unordered_set <T>>
- 26. 初始化std :: vector时出现分段错误<double>
- 27. 如何修复错误:“错误:启动必须<=结束”
- 28. 如何从索引中删除std :: vector <>中的元素?
- 29. 将std :: shared_ptr << std :: vector <double>>传递给函数
- 30. 使用boost :: assign :: list_of初始化std :: vector <std :: vector <int>>
你能使用升压? – 2010-12-05 15:29:47
是的,我可以使用boost。=) – Rella 2010-12-05 15:39:44