473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

find() syntax with obj member data

If I have a class like:

class cExample
{
private:
string name;
int id;
}

and I have a vector of these objects vector<cExample> v.

How do I use find() to get a particular id? (I know maps are better at
this but I need to use a vector)

This wouldn't work, right? vIter=find(v.begin(),v.end(),4);
Jul 19 '05 #1
1 2027

"solartimba" <ka*****@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
If I have a class like:

class cExample
{
private:
string name;
int id;
}

and I have a vector of these objects vector<cExample> v.

How do I use find() to get a particular id? (I know maps are better at
this but I need to use a vector)

This wouldn't work, right? vIter=find(v.begin(),v.end(),4);


#include <algorithm>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>

class cExample
{
std::string name;
int id;

public:
cExample(const std::string& s, int i) : name(s), id(i)
{ }

bool operator==(int i) const
{
return id == i;
}

std::ostream& out(std::ostream& os) const
{
return os << id << ' ' << name;
}
};

std::ostream& operator<<(std::ostream& os, const cExample& e)
{
return e.out(os);
}

std::vector<cExample>::const_iterator lookup
(const std::vector<cExample>& v, int id)
{
std::vector<cExample>::const_iterator it
(std::find(v.begin(), v.end(), id));

if(it != v.end())
std::cout << *it << '\n';
else
std::cout << id << " [not found]\n";

return it;
}

int main()
{
std::vector<cExample> v;

v.push_back(cExample("John", 2));
v.push_back(cExample("Paul", 4));
v.push_back(cExample("George", 6));
v.push_back(cExample("Ringo", 8));

for(int i = 0; i < 10; ++i)
lookup(v, i);

return 0;
}

-Mike
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
3
by: infinity | last post by:
Hi all, Is constructor a special member function? But I don't think it is either a member function or even a special member function although it has the syntax of a function. I think it confused...
4
by: Al_C | last post by:
OK, my K&R is about 10 years old now, I'm tring to understand the following: typedef struct { union { OPTICAL_REPORT optical; UINT8 battery_level; } shared; UINT8 combi;...
9
by: toton | last post by:
Hi, Which one is the correct syntax for constructor for static memory allocation Object obj("param"); or Object obj = Object("param"); 1) For the first one, how compiler checks it as not a...
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
10
by: Gary | last post by:
I'm trying to find a way to determin which window Within-Another-Program currently has the focus. THE SITUATION: (Skip down to "My Question" if you don't want the background...) * This is...
5
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.