counting number of elements in a vector

Page 1 of 1

2 Replies - 6718 Views - Last Post: 06 October 2009 - 11:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130268&amp;s=9dae84b9dc06fb64841d0a4786166d76&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 dbthegrandtour

Reputation: 0
• Posts: 65
• Joined: 15-December 08

counting number of elements in a vector

Posted 06 October 2009 - 11:00 AM

```#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{

vector<int> myvector;
int counter;
int x;
char filemine[30];
int choice;
int key,i;
int length;
int q;

cout << "Enter File: ";
cin >> filemine;

ifstream inFile(filemine ,ifstream::in);

if (inFile.good())
{

vector<long int> myvector;
long int num;
while( inFile >> num )
{
myvector.push_back(num);

}

inFile.close();
}
else
{
cout << "Unable to open file" << endl;
}

cout <<"  =================== " << endl;
cout << " | 1. Insertion Sort |" << endl;
cout << " | 2. Merge Sort	 |" << endl;
cout << " | 3. Quick Sort	 |" << endl;
cout << " | 4. Counting Sort  |" << endl;
cout <<"  =================== " << endl;
cout << "Choose One: ";
cin >> choice;

system("PAUSE");
return EXIT_SUCCESS;
}

```

so this is what i have so far. i have tried using a for loop to loop throught myvector[x] and just used q+=1; but that just counts to 7 when there are 10 elements. I checked and it is storing all 10 elements in the vector. I need it to count the elements in varying sizes vectors.

Is This A Good Question/Topic? 0

Replies To: counting number of elements in a vector

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: counting number of elements in a vector

Posted 06 October 2009 - 11:09 AM

Something wrong with vector's size() function?

#3 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

Re: counting number of elements in a vector

Posted 06 October 2009 - 11:47 AM

thats what im wondering?

```vector<int>stuff(20);
cout<<stuff.size()<<endl;

```