It is currently Mon Apr 24, 2017 12:24 am

All times are UTC - 5 hours




 Page 1 of 1 [ 5 posts ] 
Author Message
 Post subject: VS2010 vs VS2008
PostPosted: Mon Jan 17, 2011 10:27 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Here is a good place to post code snippets in c++ where implementations in 2010 are easier then in 2008... If you know of anything that makes life more easier by the use of c++ 2010 then please feel free to show some examples here.


Last edited by skilz80 on Thu Apr 07, 2011 1:45 am, edited 1 time in total.

Offline
 Profile  
 
 Post subject: The use of the keyword "Auto" in 2010
PostPosted: Mon Jan 17, 2011 10:33 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
Here I will show the use of the keyword "auto" in 2010... With this code snippet, this will compile and run only in vs 2010 not in 2008 or earlier...
The code should be self explanatory and the simplification, is less typing and less prone to errors.

This is just a basic empty win32 console program

#include <string>
#include <iostream>
#include <ostream>
#include <map>

using namespace std;

int main()
{
   map<int, string> m;

   m[1] = "one";
   m[2] = "two";
   m[4] = "four";
   m[3] = "three";
   m[2] = "TWO!";

   cout << "OLD Technique: using " << endl;
   cout << "for(map<int,string>::const_iterator it = m.cbegin(); it != m.cend(); ++it ){...}" << endl;
   cout << endl;
   
   for ( map<int, string>::const_iterator it = m.cbegin(); it != m.cend(); ++it )
   {
      cout << it->first << "," << it->second << endl;
   }
   
   cout << endl;
   
   cout << "NEW Technique, using ( for auto i = m.cbegin(); i != m.cend(); ++i ){...} " << endl;
   cout << endl;
   
   for ( auto i = m.cbegin(); i != m.cend(); ++i )
   {
      cout << i->first << "," << i->second << endl;
   }

   m.clear();

   return 0;
}


If you comapare the first and second for loops going through the std map they do exactly the same thing, but as you can see The second is much easier to implement as well as being less prone to errors. The use of the "auto" keyword here replaces the statement "map<int, string>::const_iterator" I hope this helps!!!


Offline
 Profile  
 
 Post subject: Re: VS2010 vs VS2008
PostPosted: Wed Nov 23, 2011 3:41 pm 

Joined: Tue Nov 22, 2011 4:59 pm
Posts: 12
the wiki states All variables declared within a block of code are automatic by default, but this can be made explicit with the auto keyword.[1] An uninitialized automatic variable has an undefined value until it is assigned a valid value of its type.

since you gave it a type iterator it seems to work. its a neat trick but im not sure id use it. it kinda reminds me of variables in lua. you have code that doesnt specify a type until you look at what is used with it.

I have nightmares of everyonebeing lazy now and writing their code like this with no type.

#include <iostream>
#include <string>
using namespace std;


int main(int argc, char** argv[])
{
   
   auto mystring = "text";
   auto mynum = 13;
   auto myfloat = 0.01825;

   cout << mystring << endl;
   cout << mynum  << endl;
   cout << myfloat << endl;

   system("pause"); // dont use this its nonportable
   return 0;
}


Offline
 Profile  
 
 Post subject: Re: VS2010 vs VS2008
PostPosted: Wed Nov 23, 2011 5:07 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I understand where you are coming from, however when you are using for loops to traverse through a vector, list map etc. the use of the auto word, simplifies your code and does make it more readable.

for ( std::vector<SomeParam>::const_iterator it = _thisVectorVariable.cbegin(); it != _thisVectorVariable.cend(); ++it ) {
// Code Here
}

vs

for ( auto i = _thisVectorVariable.cbegin(); i != _thisVectorVariable.cend(); ++i ) {
// Code Here
}

Is easier to read, It doesn't seem very intuitive in this aspect. But lets say if you had some crazy nesting of vectors and or maps
std::vector< std::vector < std::vector < std::map < int, MyClass > > > >
auto makes it much more easier to read as long as you know what the variable is.


Offline
 Profile  
 
 Post subject: Re: VS2010 vs VS2008
PostPosted: Wed Nov 23, 2011 5:35 pm 

Joined: Tue Nov 22, 2011 4:59 pm
Posts: 12
I see your point and how it can be useful. Im just curious if the tradeoff of the abuse that someone could do using it was worth it.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron