counting number of elements in a vector

I need the program to count the number of element in a vector

Page 1 of 1

2 Replies - 6436 Views - Last Post: 06 October 2009 - 11:47 AM Rate Topic: -----

#1 dbthegrandtour   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1