473,779 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Rot amerSet>::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
Jul 5 '07 #1
4 1771
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<Rot amerSet>::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::iterato r (or const_iterator) is a RandomAccessIte rator,
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<Ro tamerSet>::cons t_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<Fo om_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::iterato r (or const_iterator) is a RandomAccessIte rator,
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::iterato r (or const_iterator) is a
RandomAccessIt erator, 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.a c.ukwrote:
Victor Bazarov wrote:
>Jon Rea wrote:
>>1) How do I return a size_t from the itterator ?!?

Since the vector::iterato r (or const_iterator) is a RandomAccessIte rator,
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
3361
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5115
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
9
8897
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; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
3
3558
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
1856
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 at the program you should understand what I am trying to do easily, which is convert an array of string to an array of another type, either string, float or interger.) I am not sure what I am trying to do is legal. Obvisouly it doesn't seem...
13
2963
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
9102
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
2246
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, from the language definition point of view? const pr& p = *(const pr*)(&v); // pr - either std::pair or hand-defined pair of elements
3
5657
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 k = i+1;
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.