Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя Fear_Is_An_Illusion

Автор Fear_Is_An_Illusion, 9 лет назад, По-английски

The newer versions of C++ have introduced an auto keyword, which basically automatically sets the data type of variable. I can imagine it being useful in situations like these, where it increases coding speed and prevents unnecessary cluttering.


vector< pair <int, int> > foo; for(auto i=foo.begin(),i!=foo.end(),++i) { ..... }

However still it just seems as a small advantage. Also using it in place of int , string, etc just causes a lot of confusion, like dealing with variables in any weakly typed language, say Javascript.

var foo=13.67;

foo="it's a string"

Though using auto prevents assigning a string as an int, but using it everywhere makes debugging a nightmare

Say you shouldnt be doing this in C++

auto x="mewat1000";
auto y=5.33;

So what other useful feature it serves, which I seem to be missing out ?

Upd1 : One of my few posts which didnt get downvoted to oblivion. Rest of the posts disapper from recent blog activity list within 15-20 mins of posting them.

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

You can feel all power of keyword auto when you use it with range-based loops. Your code can be modified this way.

vector< pair <int, int> > foo;

for(auto i : foo)
{
    .....
}
  • »
    »
    9 лет назад, # ^ |
      Проголосовать: нравится +13 Проголосовать: не нравится

    In fact this is not only the power of auto. This is for loop based on range and equivalent to

    for(pair<int,int> i:foo){...}
    

    auto just makes the code shorter.

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I what do you mean by increasing speed?