By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

std::vector - the wanders of itterators and size_t

P: n/a
I have a class that has a private std::vector and uses it to store its
content. I want to search the data within this vector and return the
size_t index of that data.

Pseudo-code:

class Foo
{
public:
std::string name;
int otherData;
};

inline bool operator==( const Foo& f, const std::string& name )
{
return f.name.compare(name) == 0;
}

class Manager
{
public:
size_t getIDFor( const std::string& name )
{
std::vector<RotamerSet>::const_iterator findResult = std::find(
m_Data.begin(), m_Data.end(), name );
// How do I return a size_t from an itterator ?!?
return 0;
}
private:
std::vector<Foom_Data; // The data, the manager garantees that this
contains only ONE of each named foo
};

1) How do I return a size_t from the itterator ?!?
2) Is std::find more efficient than manually itterating the vector and
performing the == test sequentially?

Many thanks
Jon
Jul 5 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jon Rea wrote:
I have a class that has a private std::vector and uses it to store its
content. I want to search the data within this vector and return the
size_t index of that data.

Pseudo-code:

class Foo
{
public:
std::string name;
int otherData;
};

inline bool operator==( const Foo& f, const std::string& name )
{
return f.name.compare(name) == 0;
}

class Manager
{
public:
size_t getIDFor( const std::string& name )
{
std::vector<RotamerSet>::const_iterator findResult = std::find(
m_Data.begin(), m_Data.end(), name );
// How do I return a size_t from an itterator ?!?
if (findResult != m_Data.end()) return findResult - m_Data.begin();
else
??? // not found -- decide what to return.
return 0;
}
private:
std::vector<Foom_Data; // The data, the manager garantees that this
contains only ONE of each named foo
};

1) How do I return a size_t from the itterator ?!?
Since the vector::iterator (or const_iterator) is a RandomAccessIterator,
you can subtract two values (see above).
2) Is std::find more efficient than manually itterating the vector and
performing the == test sequentially?
That's what 'find' does internally. So they should be really close as
far as performance is concerned. But (or hence) there is no clear answer
to this question. You need to profile both methods to be able to compare.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 5 '07 #2

P: n/a
Victor Bazarov wrote:
Jon Rea wrote:
>I have a class that has a private std::vector and uses it to store its
content. I want to search the data within this vector and return the
size_t index of that data.

Pseudo-code:

class Foo
{
public:
std::string name;
int otherData;
};

inline bool operator==( const Foo& f, const std::string& name )
{
return f.name.compare(name) == 0;
}

class Manager
{
public:
size_t getIDFor( const std::string& name )
{
std::vector<RotamerSet>::const_iterator findResult = std::find(
m_Data.begin(), m_Data.end(), name );
// How do I return a size_t from an itterator ?!?

if (findResult != m_Data.end()) return findResult - m_Data.begin();
else
??? // not found -- decide what to return.
>return 0;
}
private:
std::vector<Foom_Data; // The data, the manager garantees that this
contains only ONE of each named foo
};

1) How do I return a size_t from the itterator ?!?

Since the vector::iterator (or const_iterator) is a RandomAccessIterator,
you can subtract two values (see above).
But doesn't 'findResult - m_Data.begin()' return an itterator, not size_t ??
>
>2) Is std::find more efficient than manually itterating the vector and
performing the == test sequentially?

That's what 'find' does internally. So they should be really close as
far as performance is concerned. But (or hence) there is no clear answer
to this question. You need to profile both methods to be able to compare.
Fair enough :-)
>
V
Jon
Jul 5 '07 #3

P: n/a
Jon Rea wrote:
Victor Bazarov wrote:
>Jon Rea wrote:
>>[..]
1) How do I return a size_t from the itterator ?!?

Since the vector::iterator (or const_iterator) is a
RandomAccessIterator, you can subtract two values (see above).

But doesn't 'findResult - m_Data.begin()' return an itterator, not
size_t ??
Huh? Why would it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 5 '07 #4

P: n/a
Jon Rea <jo*****@bris.ac.ukwrote:
Victor Bazarov wrote:
>Jon Rea wrote:
>>1) How do I return a size_t from the itterator ?!?

Since the vector::iterator (or const_iterator) is a RandomAccessIterator,
you can subtract two values (see above).

But doesn't 'findResult - m_Data.begin()' return an itterator, not size_t ??
Maybe you could try using std::distance() (found in <iterator>). It
returns a value of type iterator_traits<>::difference_type, but since
you are subtracting the beginning iterator, the value should be
non-negative and easily convertible to a size_t.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.