Use the auto keyword in C++ STL

后端 未结 6 1689
野趣味
野趣味 2020-12-23 00:10

I have seen code which use vector,

vectors;
s.push_back(11);
s.push_back(22);
s.push_back(33);
s.push_back(55);
for (vector::iterator i         


        
相关标签:
6条回答
  • 2020-12-23 00:14

    If you want a code that is readable by all programmers (c++, java, and others) use the original old form instead of cryptographic new features

    atp::ta::DataDrawArrayInfo* ddai;
    for(size_t i = 0; i < m_dataDraw->m_dataDrawArrayInfoList.size(); i++) {
        ddai = m_dataDraw->m_dataDrawArrayInfoList[i];
        //...
    }
    
    0 讨论(0)
  • 2020-12-23 00:26

    The auto keyword gets the type from the expression on the right of =. Therefore it will work with any type, the only requirement is to initialize the auto variable when declaring it so that the compiler can deduce the type.

    Examples:

    auto a = 0.0f;  // a is float
    auto b = std::vector<int>();  // b is std::vector<int>()
    
    MyType foo()  { return MyType(); }
    
    auto c = foo();  // c is MyType
    
    0 讨论(0)
  • 2020-12-23 00:26

    auto keyword is intended to use in such situation, it is absolutely safe. But unfortunately it available only in C++0x so you will have portability issues with it.

    0 讨论(0)
  • 2020-12-23 00:29

    It's additional information, and isn't an answer.

    In C++11 you can write:

    for (auto& it : s) {
        cout << it << endl;
    }
    

    instead of

    for (auto it = s.begin(); it != s.end(); it++) {
        cout << *it << endl;
    }
    

    It has the same meaning.

    Update: See the @Alnitak's comment also.

    0 讨论(0)
  • 2020-12-23 00:29

    This is new item in the language which I think we are going to be struggling with for years to come. The 'auto' of the start presents not only readability problem , from now on when you encounter it you will have to spend considerable time trying to figure out wtf it is(just like the time that intern named all variables xyz :)), but you also will spend considerable time cleaning after easily excitable programmers , like the once who replied before me. Example from above , I can bet $1000 , will be written "for (auto it : s)", not "for (auto& it : s)", as a result invoking move semantics where you list expecting it, modifying your collection underneath .

    Another example of the problem is your question itself. You clearly don't know much about stl iterators and you trying to overcome that gap through usage of the magic of 'auto', as a result you create the code that might be problematic later on

    0 讨论(0)
  • 2020-12-23 00:36

    The auto keyword is simply asking the compiler to deduce the type of the variable from the initialization.

    Even a pre-C++0x compiler knows what the type of an (initialization) expression is, and more often than not, you can see that type in error messages.

    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        vector<int>s;
        s.push_back(11);
        s.push_back(22);
        s.push_back(33);
        s.push_back(55);
        for (int it=s.begin();it!=s.end();it++){
            cout<<*it<<endl;
        }
    }
    
    Line 12: error: cannot convert '__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, __gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >' to 'int' in initialization
    

    The auto keyword simply allows you to take advantage of this knowledge - if you (compiler) know the right type, just choose for me!

    0 讨论(0)
提交回复
热议问题