C + + Auto

Source: Internet
Author: User

The C++14 standard has recently been passed, as before, without much change to the language, and the C++14 standard is to make it easier for programmers to program by improving C++11, c++11 introduce the Auto keyword (strictly speaking, auto starts with C + + 03, just C + + 11 changed the meaning of auto), Auto makes your code cleaner and easier for you to avoid mistakes, for example

That's what you have to write.

12 inti = 1;   std::complex<double> c = return_a_complex_number();

You can write that now.

12 auto i = 1;   auto c = return_a_complex_number();

Variables declared as auto are allocated memory at compile time, not to run time, so using auto no longer raises any speed delay, which means that when using auto, this variable is not initialized. Because the compiler cannot know the type of this variable.

1 auto i;     // this will rise a compilation error

One of the bad things about c++11 is that you can't use auto to define a function type, and in the new standard you can:

12345 // Error in C++11, works in C++14  auto my_function() {      ...      returnvalue;  }

As soon as your function returns a value, the compiler will know how to interpret the Auto keyword. Now you can use the latest version of Clang and GCC,

1 g++-4.9.1-Wall -std=c++14-pedantic my_prog_name.cpp -o my_prog_name

To make it easier to use Auto to simplify your code, let's use c++98, c++11, and c++14 to implement the same piece of code separately, to illustrate, we use two functions to change a vector variable

1 multiply_by_two(add_one(my_vector));

Obviously, this loop gives each value of a vector variable multiplied by two and you can write a function instead of two. We are not here to pursue performance, but to illustrate the use of auto.

You're going to write that in c++98.

std::vector<int>& Add_one (std::vector<int> &v) {for        (std::vector<int>::iterator it = V.begin (); It! = V.end (); it++) {            *it + = 1;        }        return v;    }    void Multiply_by_two (std::vector<int> &v) {for        (std::vector<int>::iterator it = V.begin (); it! = V.end (); it++) {            *it *= 2;        }    }

The code above looks very verbose.

You can write that in c++11.

123456789101112 std::vector<int>& add_one(std::vector<int> &v) {        for(auto& it : v) {            it += 1;        }        return v;    }     void multiply_by_two(std::vector<int> &v) {        for(auto& it : v) {            it *= 2;        }        }

There is obviously progress in c++11, and this is a little bit of code that we can use auto to simplify the loop. But still more verbose.

In c++14 you can use auto to define a function type, and the code can be simplified to:

123456789101112 auto& add_one(std::vector<int>& v) {      for(auto& it : v) {          it += 1;      }      return v;  }  void multiply_by_two(std::vector<int>& v) {      for(auto& it : v) {          it *= 2;      }      }

Here is the complete code

1234567891011121314151617181920212223242526272829303132 // C++14 "auto" demo code     #include <iostream> #include <vector> auto& add_one(std::vector<int>& v) {     for(auto& it : v) {         it += 1;     }     return v; } void multiply_by_two(std::vector<int>& v) {     for(auto& it : v) {         it *= 2;     }     } void print_vec(const std::vector<int>& v) {     for(const auto& e: v) {         std::cout << e << std::endl;     }        std::cout << std::endl;      } int main() {     // Add one and multiply by two a vector of integers     std::vector<int> my_vector{-1, 2, 3, 5};     multiply_by_two(add_one(my_vector));         print_vec(my_vector);     return 0; }

You can clearly contrast that c++14 has made a little progress over c++11.

C + + Auto

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.