I started reading about these vectors a little while ago and I had a question about data types of a vector and using vector functions like .size() or .capacity()
You will notice the they change the vector from type INT to Long for the .size() function. They do the same for other programs also on that page.
I tried running the program with data type INT and it seemed to work fine. I was wondering if it made a difference if I used int or is Long a recommended data type for using these vector functions. I'm not sure if it might make a difference if I start using huge vectors but int seems to hold a lot of numbers as it is.
I guess I'm not sure if it makes a difference using long these days. I know it has something to do with compatibility for 32 / 64 bit (I think?)
#include <iostream>
#include <vector>
usingnamespace std;
int main(int argc, char** argv) {
/* Initialize vector of 10 copies of the integer 5 */
vector<int> vectorOne(10,5);
/* Display size of vector */
cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
/* run through the vector and display each element, using size() to determine index boundary */
for (long index=0; index<(long)vectorOne.size(); ++index) {
cout << "Element " << index << ": " << vectorOne.at(index) << endl;
}
return EXIT_SUCCESS;
}
Whether you use long or int as your vector type depends solely on what you're going to use the vector for. If you are storing the mass of planets, for example, you will want to use long. However, for most everything an int will suffice.
Those arguments are used when you run the program from the command line. When running the program you can pass arguments into the program while you run it and argc and argv are those.
The first one, argc, is the argument counter. It is an integer that tells the program how many arguments there will be.
The second one, argv is the argument vector. It contains all of the arguments being passed in up to the amount defined in argc.
All in all, you don't need to worry about it. Most people typically make their main() function with zero parameters.
Say for example I want to play a game. However, the game has to be launched through a launcher. So, to play the game I start the launcher and then the launcher updates the game or whatever it needs to do and then that program starts the game. So we have a program starting another program.
In this case, the launcher might detect what resolution your screen is, what processor you have, etc and then it passes this data into the game as it launches so it knows what settings to run on. That could be one possible use of those arguments. Most people on a basic level don't really use them.
They are not changing the vector's element type. They are taking the result of vector::size() (which normally has the type std::size_t) and casting it to the type long for no good reason.