x******a 发帖数: 6336 | 1 在visual studio express 2012里为什么compile不过去?
#include
#include
#include
#include
int main()
{
std::vector vec{3, 4, 2, 9, 15, 267};
for(auto& elem:vec) elem*=3;
std::ostream_iterator out_it(std::cout, ", ");
std::copy(vec.begin(), vec.end(), out_it);
return 0;
}
Error 1 error C2601: 'vec' : local function definitions are illegal
c:users。。documentscodetest11test11main.cpp 8 1 test11
Error 2 error C2143: syntax error : missing ';' before '}' c:users
。。documentscodetest11test11main.cpp 8 1 test11
Error 3 error C2065: 'vec' : undeclared identifier c:users。。
documentscodetest11test11main.cpp 9 1 test11
Error 4 error C3312: no callable 'begin' function found for type ''
unknown-type'' c:users。。documentscodetest11test11main.cpp 9 1
test11
Error 5 error C3312: no callable 'end' function found for type ''
unknown-type'' c:users。。documentscodetest11test11main.cpp 9 1
test11
Error 6 error C2065: 'elem' : undeclared identifier c:users。。
documentscodetest11test11main.cpp 9 1 test11
Error 7 error C2065: 'vec' : undeclared identifier c:users。。
documentscodetest11test11main.cpp 11 1 test11
Error 8 error C2228: left of '.begin' must have class/struct/union
c:users。。documentscodetest11test11main.cpp 11 1 test11
Error 9 error C2228: left of '.end' must have class/struct/union c:
users。。documentscodetest11test11main.cpp 11 1 test11
10 IntelliSense: expected a ';' c:Users。。
DocumentsCodetest11test11main.cpp 8 22 test11 | m*******l 发帖数: 12782 | 2 studio express 2012 does not support initializer_list originally
for std::vector
for they have a commnunity preview versionnnnnnnnn has that
maybe you should try 2013 which is out last month.
【在 x******a 的大作中提到】 : 在visual studio express 2012里为什么compile不过去? : #include : #include : #include : #include : int main() : { : std::vector vec{3, 4, 2, 9, 15, 267}; : for(auto& elem:vec) elem*=3; : std::ostream_iterator out_it(std::cout, ", ");
| m*******l 发帖数: 12782 | 3 VS 2012 Nov CTP compiler has that suppport
【在 m*******l 的大作中提到】 : studio express 2012 does not support initializer_list originally : for std::vector : for they have a commnunity preview versionnnnnnnnn has that : maybe you should try 2013 which is out last month.
| m*******l 发帖数: 12782 | 4 it compiles in 2013 ultimate preview
【在 x******a 的大作中提到】 : 在visual studio express 2012里为什么compile不过去? : #include : #include : #include : #include : int main() : { : std::vector vec{3, 4, 2, 9, 15, 267}; : for(auto& elem:vec) elem*=3; : std::ostream_iterator out_it(std::cout, ", ");
| x******a 发帖数: 6336 | 5 thanks guys.
downloading the 2013 version now... | x******a 发帖数: 6336 | 6 接着请教: 下面两个expressions,第一个生成的vec的element都一样,第二个生成的
element是随机的.除了用getNormal(std::mt19937&), 有什么办法使得第一个生成的是
随机的?
1. std::fill_n(std::back_inserter(vec), 10, getNormal());
2. for (auto i = 0; i != 10; ++i) vec.push_back(getNormal());
double getNormal()
{
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> normal(0, 1);
return normal(gen);
} | m*******l 发帖数: 12782 | 7 1. getNormal() has been called only once
【在 x******a 的大作中提到】 : 接着请教: 下面两个expressions,第一个生成的vec的element都一样,第二个生成的 : element是随机的.除了用getNormal(std::mt19937&), 有什么办法使得第一个生成的是 : 随机的? : 1. std::fill_n(std::back_inserter(vec), 10, getNormal()); : 2. for (auto i = 0; i != 10; ++i) vec.push_back(getNormal()); : double getNormal() : { : std::random_device rd; : std::mt19937 gen(rd()); : std::normal_distribution<> normal(0, 1);
| p***o 发帖数: 1252 | 8 Use std::generate_n instead of std::fill_n.
【在 x******a 的大作中提到】 : 接着请教: 下面两个expressions,第一个生成的vec的element都一样,第二个生成的 : element是随机的.除了用getNormal(std::mt19937&), 有什么办法使得第一个生成的是 : 随机的? : 1. std::fill_n(std::back_inserter(vec), 10, getNormal()); : 2. for (auto i = 0; i != 10; ++i) vec.push_back(getNormal()); : double getNormal() : { : std::random_device rd; : std::mt19937 gen(rd()); : std::normal_distribution<> normal(0, 1);
| x******a 发帖数: 6336 | 9 moneybull: yes I understand now. thank you.
pptwo: the following does not work. any suggestions are appreciated.
std::vector vec;
vec.resize(10);
std::generate_n(vec, 10, getNormal);
error C4996: 'std::_Generate_n': Function call with parameters that may be
unsafe - this call relies on the caller to check that the passed values are
correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See
documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\
algorithm(1542) : see declaration of 'std::_Generate_n'
1> c:\users\..\documents\visual studio 2013\projects\testc11\
testc11\main.cpp(9) : see reference to function template instantiation '_
OutIt std::generate_n>,int,double>(_
OutIt,_Diff,_Fn0)' being compiled
1> with
1> [
1> _OutIt=std::vector>
1> , _Ty=double
1> , _Diff=int
1> , _Fn0=double
1> ] |
|