由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教C++11
相关主题
template 疑问程序怎么不认识class, namespace等identifier呀?
core java里有跟C++ std::async类似的东西吗?C++ Q03:
C++并发和Java并发有多大区别?C++ Q07: unnamed namespace
C++11的lambda不会破坏可读性吗?operator overloading (C++)
How to run Message Passing Interface C file?又一个初级问题: C++中多如牛毛的#define格式
Any difference between class and typename identifier?深情的呼唤师傅们!C++做题做不出来啦!
i +++ jjavascript function-ask for help
【请教】mpicc 和 mpiCC编译问题python下的expect
相关话题的讨论汇总
话题: error话题: test11话题: std话题: users话题: vec
进入Programming版参与讨论
1 (共1页)
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> ]
1 (共1页)
进入Programming版参与讨论
相关主题
python下的expectHow to run Message Passing Interface C file?
Static variables in functionAny difference between class and typename identifier?
哪位同修能帮我测试一下i +++ j
问题一枚【请教】mpicc 和 mpiCC编译问题
template 疑问程序怎么不认识class, namespace等identifier呀?
core java里有跟C++ std::async类似的东西吗?C++ Q03:
C++并发和Java并发有多大区别?C++ Q07: unnamed namespace
C++11的lambda不会破坏可读性吗?operator overloading (C++)
相关话题的讨论汇总
话题: error话题: test11话题: std话题: users话题: vec