473,386 Members | 1,798 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.

std::vector - the wanders of itterators and size_t

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
4 1739
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
3
by: Jon Rea | last post by:
Is there a way of making a std::vector, or indeed any other std container that is indexed by something larger than a 'size_t' i.e. an unsigned long. Thanks, Jon Rea
3
by: mast2as | last post by:
sorry i am too sure how to write a more explicit subject but this code doesn't compile for the type string and I am not sure why (and I am not sure either how to describe the problem but by looking...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
2
by: Markus Dehmann | last post by:
Hi, I'd like to hash std::vector<intobjects using hash_set, but I'm not sure what hash function to use. Does anybody have a suggestion? Thank you! Markus
3
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so,...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.