A simple question about vectors

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()

This is the example I read about here.
http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

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?)

sample code from that link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>

using namespace 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;
}
Last edited on
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.
Thanks for the explanation. One other thing I didn't really understand was the main function declaration.

int main(int argc, char** argv) {Code Here}

What does it mean when you use argc and argv?
Last edited on
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.

1
2
3
4
int main()
{
    ...
}
ahh, is that for multi-threading (or whatever the correct term is)?

It would "loop" through the main function argc times and go through each value in argv?

I probably should read about that more but I don't really have a need to do multithreading yet. Still learning a lot of the basic stuff heh.
No it's not for multi-threading.

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.
ahh thank you for the explanation. I will keep that in mind should I need something like that in the future.
Anytime. :)
index<(long)vectorOne.size()

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.
Last edited on
Topic archived. No new replies allowed.