I saw this example while i am searching about vectors..

``````vector<double> student_marks;
// no size specified: vector contains
// no elements

int num_students;
cout << "Number of students: " << flush;
cin >> num_students;

student_marks.resize (num_students);

for (vector<double>::size_type i = 0; i < num_students; i++)
{
cout << "Enter marks for student #" << i+1
<< ": " << flush;
cin >> student_marks[i];
}``````

cant we use

``for (int i = 0; i < num_students; i++)``

``for (vector<double>::size_type i = 0; i < num_students; i++)``

## All 11 Replies

I saw this example while i am searching about vectors..

``````vector<double> student_marks;
// no size specified: vector contains
// no elements

int num_students;
cout << "Number of students: " << flush;
cin >> num_students;

student_marks.resize (num_students);

for (vector<double>::size_type i = 0; i < num_students; i++)
{
cout << "Enter marks for student #" << i+1
<< ": " << flush;
cin >> student_marks[i];
}``````

cant we use

``for (int i = 0; i < num_students; i++)``

``for (vector<double>::size_type i = 0; i < num_students; i++)``

num_students is an int, so you shouldn't use vector<double>::size_type. size_type is an unsigned type, and int is signed. It's not usually a good idea to mix signed and unsigned types.

If you use the size of the vector as a loop condition instead, then size_type would be best:

``for (vector<double>::size_type i = 0; i < student_marks.size(); i++)``

num_students is an int, so you shouldn't use vector<double>::size_type. size_type is an unsigned type, and int is signed. It's not usually a good idea to mix signed and unsigned types.

If you use the size of the vector as a loop condition instead, then size_type would be best:

``for (vector<double>::size_type i = 0; i < student_marks.size(); i++)``

ok thanks.
Are arrays and vectors the same?

Are arrays and vectors the same?

No.

then what's the difference?
both are used for binding a collection of values of similar type
So what's the difference?

An array is just many of the same object next to each other in memory.

A vector is a proper C++ container class with an accompanying set of functions for interrogation and manipulation, able to manage its own resources, and to which numerous other C++ facilities such as the algorithms library can be applied.

able to manage its own resources,.

can u please elaborate this one please?

able to manage its own resources

i saw this statement
Vectors have one important advantage with respect to C-style arrays: vectors can be resized during the execution of the program to accommodate any extra elements as needed, or even to “shrink” the vector.
Is this correct?
If this is correct even arrays size in c can be manipulated using realloc() function..
Please clarify this one.

Is this correct?

Yes, that's the biggest reason why vectors are recommended over arrays.

If this is correct even arrays size in c can be manipulated using realloc() function..

Arrays and pointers are different. realloc() only works with pointers that were previously allocated memory by malloc(), calloc(), or realloc(). Arrays have a compile time fixed size.

Yes, that's the biggest reason why vectors are recommended over arrays.

Arrays and pointers are different. realloc() only works with pointers that were previously allocated memory by malloc(), calloc(), or realloc(). Arrays have a compile time fixed size.

arrays decay into pointers..
so arrays are can also be treated as pointer..
If we consider..
int a;
'a' represent the starting address of the array which indeed means that it is a pointer..
Isnt right?

arrays decay into pointers..

Only most of the time. A common misconception is that arrays are equivalent to pointers because of that behavior.

If we consider..
int a;
'a' represent the starting address of the array which indeed means that it is a pointer..

http://www.torek.net/torek/c/pa.html

Only most of the time. A common misconception is that arrays are equivalent to pointers because of that behavior.

ok thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.