std::vector - the wanders of itterators and size_t

J

Jon Rea

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<Foo> m_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
 
V

Victor Bazarov

Jon said:
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<Foo> m_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
 
J

Jon Rea

Victor said:
if (findResult != m_Data.end()) return findResult - m_Data.begin();
else
??? // not found -- decide what to return.


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 ??
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 :)

Jon
 
V

Victor Bazarov

Jon said:
Victor said:
Jon said:
[..]
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
 
M

Marcus Kwok

Jon Rea said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top