您最喜欢的 C++ 是什么?成语?
可能的重复:
您最喜欢的 C++ 编码风格习惯用法是什么
亲爱的 C++ 粉丝,
您能在这里列出您最喜欢的 C++ 习惯用法吗?最好列出那些有用但不为人所知的。
首先,我放了我的一个:
为了避免繁琐的重复 for (size_t i = 0; i < n; ++i) {...}
,我使用这样的宏:
#define LOOP(n) for (size_t _i = 0; _i < n; ++_i)
_i is绑定使用的占位符。
所以我可以写:
vector<int> coll(100);
LOOP (100)
{
coll[_i] = _i;
}
LOOP (100)
{
auto a = _i;
LOOP (100)
{
auto b = _i;
cout << a << b;
}
}
Possible Duplicate:
What are your favorite C++ Coding Style idioms
Dear C++ fans,
Could you list here your favorite C++ idioms? It is better to list those which are useful yet not well-known.
Firstly, I put one of mine:
To avoid tedious repeating for (size_t i = 0; i < n; ++i) {...}
, I use a macro like this:
#define LOOP(n) for (size_t _i = 0; _i < n; ++_i)
_i is a placeholder as bind uses.
So I can write:
vector<int> coll(100);
LOOP (100)
{
coll[_i] = _i;
}
LOOP (100)
{
auto a = _i;
LOOP (100)
{
auto b = _i;
cout << a << b;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
RAII 位于我的列表的顶部。在很多情况下它都会派上用场......
作为通用实现最有用,例如 Andrei Alexandrescu 的
RAII is on the top of my list. There are so many cases when it just comes in handy...
Most useful as a generic implementation like Andrei Alexandrescu's libloki.
SFINAE
SFINAE
好问题,但我认为你的例子很糟糕!你的
LOOP
宏并没有让任何东西变得更清晰 - 它实际上向不使用你的宏的程序员隐藏了你的意图,而且它甚至看起来不像 C++ - 我对你的代码片段的第一反应是“这还是C++吗?”或者“普通的for
有什么问题吗?”。一遍又一遍地编写循环可能很乏味,但我们都知道每个人花在阅读和维护代码上的时间比编写代码的时间要多得多,因此编写完整的for< 并不是浪费时间。 /code>,这样就清楚多了。您可以继续争论您的观点,但您应该意识到,以这种方式使用宏不是一种被广泛接受的技术 - 其他查看您代码的人也会对您提出批评。
无论如何,添加一个实际的答案 - 我在 C++0x 中最喜欢的习惯用法是 unique_ptr 的向量:
它具有以下优点 - 本质上是一个安全的指针向量:
erase()
也会释放内存std::move()
指针移出容器并将它们放在其他地方它确实有一个或两个缺点:
(*i)-> member
语法 - 但我认为这没什么大不了的但是,特别是对于重量级对象,我认为它几乎是一个理想的容器。
Good question but I think your example is a bad one! Your
LOOP
macro does not make anything clearer - it actually hides your intent from programmers who don't use your macro, and it doesn't even look like C++ - my first reaction to your snippet would be "is that even C++?" or "what's wrong with an ordinaryfor
?". It may be tedious to write the loops over and over, but we all know everyone spends a lot more time reading and maintaining code than writing it in the first place, so it's hardly a waste of time writing out a fullfor
, and it's much clearer. You can continue to argue your point, but you should be aware that using macros in that way is not a widely accepted technique - other people looking at your code will call you out on it too.Anyway, to add an actual answer - my favourite idiom in C++0x is a vector of unique_ptr:
It has the following advantages - essentially a safe vector of pointers:
T
s and store them)erase()
on an element also releases the memorystd::move()
pointers out of the container and put them somewhere elseIt does have one or two disadvantages:
(*i)->member
syntax - but I don't think it's a big dealHowever, especially for heavyweight objects, I think it is nearly an ideal container.
RAII + 如果需要精确的生命周期范围,则打开嵌套块:
RAII + opening a nested block if exact lifetime scope needed: