Solved

C++ Vectors

Posted on 2010-09-01
4
473 Views
Last Modified: 2012-05-10
I was looking at figuring out what the quickest way of 'finding' a value in vectors are.

For example, when I scan through all my ships in game, I'll do something like:

      const ShipClass *ship = &game->ship[0];
      const ShipClass* end1 = ship + Nobj;
      while (ship != end1)
      {
            if (ship && ship->active) {
                                      //DO STUFF HERE
            }
            ship++;
      }

I'm wondering to 'find', I need to do something similar, going through an array and matching it with what I'm looking for, like:

              int findthis = 30;
      const ShipClass *ship = &game->ship[0];
      const ShipClass* end1 = ship + Nobj;
      while (ship != end1)
      {
            if (ship && ship->id == findthis) {
                                      //DO STUFF HERE
            }
            ship++;
      }


Rather, I am wondering if this is the fastest possible solution for this, or if there is a better/quicker method for finding a value in a vector list.

Any suggestions/tips? Thanks!
0
Comment
Question by:Valleriani
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 33581603
0
 
LVL 12

Expert Comment

by:w00te
ID: 33581865
This is from memory, but it should work for a simple function.  It's an example of the kind of things evilrix is pointing out.
It uses a template function though, you just pass in the int value you want to search for as a template parameter and it looks for it.  You could adjust this to take in any value (including another Ship object), and you can perform whatever comparisons you want in the function.  Find if will always give the iterator to the first position the said value was found in.
Most of the code is just there to set up the vector, the find if function accomplishes pretty much everything :)
Enjoy,
(PS, I compiled it on linux but i had to type it on windows where there istn a compiler, so if theres a typo just try and fix it, the idea works ;) )
-w00te

#include <iostream>

#include <vector>

#include <algorithm>



using namespace std;



struct Ship

{

	int id;

	Ship(int x) : id(x) {}

};



int main()

{

	template <int T>

	bool FindCondition(const Ship& ship) {return ship.id == T; }



	vector<Ship> sVec;

	sVec.push_back(Ship(12));

	sVec.push_back(Ship(15));



	vector<Ship>::iterator iter = find_if(sVec.begin(), sVec.end(), FindCondition<15>);



	if (iter != sVec.end())

		cout<<iter->id<<" was found." <<endl;

}

Open in new window

0
 
LVL 12

Assisted Solution

by:w00te
w00te earned 250 total points
ID: 33582179
Sorry about that, tried to compile now, i pushed the template in main accidentally hehe.  Here's the working code:
 template <int T>
 bool FindCondition(const Ship& ship) {return ship.id == T; }

#include <iostream>

#include <vector>

#include <algorithm>



using namespace std;



struct Ship

{

	int id;

	Ship(int x) : id(x) {}

};



template <int T>

bool FindCondition(const Ship& ship) {return ship.id == T; }



int main()

{

	vector<Ship> sVec;

	sVec.push_back(Ship(12));

	sVec.push_back(Ship(15));



	vector<Ship>::iterator iter = find_if(sVec.begin(), sVec.end(), FindCondition<15>);



	if (iter != sVec.end())

		cout<<iter->id<<" was found." <<endl;

}

Open in new window

0
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 33584030
Thank you both!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 189
groupNoAdj 7 85
split53 challenge 7 77
Is there any way to copy all SSRS reports/datasets/data sources  to new server? 3 52
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now