473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<>:: iterator and recognizable bounds

Hi folks.

I have a function that takes an element in a vector as
argument. The naive interface goes as

float computeSomethin g(const std::vector<flo at>& v, size_t i)
{
size_t j = i-1;
size_t k = i+1;

if (j<0){/* Handle start boundary condition */}
if (k>=v.size()){/* Handle terminal boundary condition */}
}

I would like to implement this in terms of iterators.
But it seems one needs several iterators to get this
done:

typedef std::vector<flo at>::const_iter ator pos;

float computeSomethin g(pos i, pos b, pos e)
// Call function with b=v.begin(), e=v.end()
{
pos j = i; --j;
pos k =i; ++k;

if (i==b){/* Handle start boundary condition */}
if (k==e){/* Handle terminal boundary condition */}
}

So my question is: Do there exist generally recognizable
invalid values for the std::vector<>:: iterators? Like std::string::np os
() ?

If yes I could make a greatly simplified (as well as
safer) interface to the function:

float computeSomethin g(pos i)
{
pos j=i;--j;
pos k = i;++k;

if(j==std::vect or::npos){}
if(k==std::vect or::npos) {}
}

But I can't find any analogy to std::string::np os defined for
vectors. Does such a value exist?

Rune
Nov 20 '08 #1
3 5636
On 2008-11-20 08:42:01 -0500, Rune Allnor <al****@tele.nt nu.nosaid:
>
So my question is: Do there exist generally recognizable
invalid values for the std::vector<>:: iterators? Like std::string::np os
() ?
No. Iterators designate sequences, so they usually come in pairs.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 20 '08 #2
KK
On Nov 20, 5:42*am, Rune Allnor <all...@tele.nt nu.nowrote:
Hi folks.

I have a function that takes an element in a vector as
argument. The naive interface goes as

float computeSomethin g(const std::vector<flo at>& v, size_t i)
{
* *size_t j = i-1;
* *size_t k = i+1;

* *if (j<0){/* Handle start boundary condition */}
* *if (k>=v.size()){/* Handle terminal boundary condition */}

}

I would like to implement this in terms of iterators.
But it seems one needs several iterators to get this
done:

typedef std::vector<flo at>::const_iter ator pos;

float computeSomethin g(pos i, pos b, pos e)
// Call function with b=v.begin(), e=v.end()
{
* * pos j = i; --j;
* * pos k =i; ++k;

* * if (i==b){/* Handle start boundary condition */}
* * if (k==e){/* Handle terminal boundary condition */}

}

So my question is: Do there exist generally recognizable
invalid values for the std::vector<>:: iterators? Like std::string::np os
() ?

If yes I could make a greatly simplified (as well as
safer) interface to the function:

float computeSomethin g(pos i)
{
* * pos j=i;--j;
* * pos k = i;++k;

* * if(j==std::vect or::npos){}
* * if(k==std::vect or::npos) {}

}

But I can't find any analogy to std::string::np os defined for
vectors. Does such a value exist?

Rune
/* apply const attribute where applicable */
float computeSomethin g(vector<floatv ec, vector<float>:: iterator
iter)
{
vector<float>:: iterator fIter = iter, bIter =iter;
if (iter != vec.begin() || iter != vec.end())
{
advance(fIter,+ 1);
advance(bIter,-1);
}
else {/* fill this up */ }
if (bIter == vec.begin())
{/* Handle start boundary condition */
}
if (fIter == vec.end())
{/* Handle terminal boundary condition */
}
}
Nov 20 '08 #3
On Nov 20, 2:42 pm, Rune Allnor <all...@tele.nt nu.nowrote:
I have a function that takes an element in a vector as
argument. The naive interface goes as
float computeSomethin g(const std::vector<flo at>& v, size_t i)
{
size_t j = i-1;
size_t k = i+1;
if (j<0){/* Handle start boundary condition */}
if (k>=v.size()){/* Handle terminal boundary condition */}
}
I would like to implement this in terms of iterators. But it
seems one needs several iterators to get this done:
Yep. That's part of the design of iterators---never use one
parameter when two can do. It makes a lot of things very
painful.
typedef std::vector<flo at>::const_iter ator pos;
float computeSomethin g(pos i, pos b, pos e)
// Call function with b=v.begin(), e=v.end()
{
pos j = i; --j;
pos k =i; ++k;

if (i==b){/* Handle start boundary condition */}
if (k==e){/* Handle terminal boundary condition */}
}
That's more or less an exact equivalent of your original
version, yes. Now if you make your function a template, it will
work with any sequence which is defined by bidirectional
iterators.
So my question is: Do there exist generally recognizable
invalid values for the std::vector<>:: iterators? Like
std::string::np os () ?
No.
If yes I could make a greatly simplified (as well as
safer) interface to the function:
float computeSomethin g(pos i)
{
pos j=i;--j;
pos k = i;++k;
if(j==std::vect or::npos){}
if(k==std::vect or::npos) {}
}
In other words, you want the iterator to encapsulate both the
current position and both upper and lower bounds somehow. It's
certainly possible, but I'm not sure what you'd gain by it.
But I can't find any analogy to std::string::np os defined for
vectors. Does such a value exist?
No, although you could probably define one arbitrarily.
Something like static_cast< size_t >( -1 ). Like
std::string::np os, it would be an index, however, and not an
iterator.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 21 '08 #4

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

Similar topics

10
7066
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
2
4764
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
6
3774
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings; strings.push_back("abc"); strings.push_back("xyz"); strings.push_back("lmnop");
4
2399
by: John | last post by:
I have a function declaration that gives an error while compiling. Can anyone help me figure this one out? inline void create(const std::vector< myclass >& plist, std::vector< myclass >::iterator left, std::vector< myclass >::iterator right); x.hpp:153: error: `class std::vector<myclass, D>, std::alloca
9
8894
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;
8
5359
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it possible to "spoof" std::vector into implementing a "true" vector of bool rather than the specialisation? Say I do: typedef bool boolreally; std::vector<booleallybvec;
4
20039
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
7
5763
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me help, thank you. I am a freshman about the STL. The following is the code. #include <set> #include <iostream> #include <vector> int main() {
8
3608
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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...
1
6177
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
4173
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...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
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.