C++11, make simple things simple

Source: Internet
Author: User

Joe Armstrong said a word:

The problem with object-oriented programming languages is that it always comes with all the implicit environments it needs. You want a banana, but you get a gorilla with a banana, and the whole jungle.

Obviously, this complicates the simple question. We do not deny that there is some code in this world that cannot be simplified. For those code that can be simplified, we hope we don't complicate it. If you are such a person, there is no reason not to use c++11. Here's a demonstration of how c++11 miraculously simplifies the code.

Let's say the following code is used to determine if there is a string within the container that meets our requirements:

vector<string>::const_iterator cit = vs.cbegin();for (; cit != vs.end(); ++cit) {    if (lookForParttern(*cit))        return true;}return false;

For c++11, this code is normal. With C++11, it's easy to write the following code:

for (auto cit = vs.cbegin(); cit != vs.end(); ++cit) {    if (lookForParttern(*cit))        return true;}return false;

For the inside of the thing or put it in better, this code than the previous paragraph, looks elegant. C + + has a range-for, so we can also write this:

for (auto& x : vs) {    if (lookForParttern(x))        return true;}return false;

Found a lot of brevity. This is not enough, we can also use the algorithm to make the code more concise:

return find_if(cbegin(vs), cend(vs), lookForParttern) != cend(vs);

Well, it becomes a line of code. Frankly speaking, Cbegin,cend is very annoying to write more. We can eliminate it by using the range algorithm:

return find_if(vs, lookForParttern) != cend(vs);

Compared with the first piece of code, not only the code is much shorter, but also more readable. The c++11 greatly increases productivity without reducing operational efficiency. You can throw the java,c# aside.


C++11, make simple things simple

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.