472,951 Members | 2,301 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

accessing contiguous std::vector elements as a pair

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[i]);

// pr - either std::pair or hand-defined pair of elements
// v - vector instance
// i + 1 < v.size()
Dec 20 '07 #1
3 2199
On Dec 20, 6:45 pm, n.torrey.pi...@gmail.com wrote:
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[i]);

// pr - either std::pair or hand-defined pair of elements
// v - vector instance
// i + 1 < v.size()
When is it safe to do so? whenever you take the appropriate
precautions.
In this case, std::vectors have an at(...) member function that throws
an out_of_range exception should someone accidentally attempt to
access something out of bounds. There you go - problem solved.

Using pointers for that has no safety and usually demands considerably
more work (ie: maintenance).

In the following, i'm not too worried about getting main's for-loop
wrong, it'll throw if i do. Try it.

#include <iostream>
#include <vector>
#include <stdexcept>

template< typename T >
class Container
{
std::vector< T m_v;
public:
// default ctor
Container() : m_v() { }
Container(const size_t sz, const T& t) : m_v(sz, t) { }
// member functions
void push_back(const T& t) { m_v.push_back(t); }
void showpair(const size_t index) const
{
std::cout << "m_v[" << index << "] ";
std::cout << m_v.at(index);
std::cout << "\tm_v[" << index + 1 << "] ";
std::cout << m_v.at(index + 1);
std::cout << std::endl;
}
size_t size() const { return m_v.size(); }
};

int main()
{
try
{
Container< int container(5, 99);
container.push_back(77);
// use i < container.size() to throw an exception
for(size_t i = 0; i < container.size() - 1; ++i)
{
container.showpair(i);
}
}
catch(const std::exception& e)
{
std::cout << "\nError: ";
std::cout << e.what() << std::endl;
}
}

/*
m_v[0] 99 m_v[1] 99
m_v[1] 99 m_v[2] 99
m_v[2] 99 m_v[3] 99
m_v[3] 99 m_v[4] 99
m_v[4] 99 m_v[5] 77
*/


Dec 21 '07 #2
On Dec 20, 5:22 pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 20, 6:45 pm, n.torrey.pi...@gmail.com wrote:
I'd like to be able to view two contiguous elements of a vector as a
pair.
void showpair(const size_t index) const
{
std::cout << "m_v[" << index << "] ";
std::cout << m_v.at(index);
std::cout << "\tm_v[" << index + 1 << "] ";
std::cout << m_v.at(index + 1);
std::cout << std::endl;
}


:-)

This is a joke, right?

If not, I didn't mean "view" in the common sense, and "at()" doesn't
add any safety if the index is already guaranteed to be in [0 ..
size()-2]
Dec 21 '07 #3
On Dec 21, 12:45 am, n.torrey.pi...@gmail.com wrote:
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[i]);
// pr - either std::pair or hand-defined pair of elements
// v - vector instance
// i + 1 < v.size()
Anytime your implementation gives you a special guarantee that
this will work. It's undefined behavior according to the
standard (even though it's likely to work most of the time in a
lot of implementations).

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

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

Similar topics

10
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...
4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
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...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
4
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.