By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,550 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

erasing element in a vector of pair ..

P: n/a

For starters,
Happy New Year to all!!

I created a vector of pairs where pair first is a primitive and pair
second is a vector of ints.

So now:

# include <iostream>
# include <vector>
# include <algorithm>

using namespace std;

typedef std::pair<int, vector<int> > int_vec_pair;
typedef std::pair<int, int > int_pair;
typedef std::vector<int_vec_pair> vec_int_vec_pair;
typedef std::vector<int_pair> vec_int_pair;
typedef vec_int_vec_pair::iterator vec_int_vec_pair_it;

int main()
{
int const proc_id = 5; // we have more of these/hardcode 1 for test
purposes.

typedef vector<int> VEC_INT;
VEC_INT size_vec;
size_vec.reserve(8);
size_vec.push_back(0x100000);
size_vec.push_back(0x200000);
size_vec.push_back(0x300000);
size_vec.push_back(0x400000);

vec_int_vec_pair v1;
v1.push_back(std::make_pair(proc_id, size_vec));
// will be doing push_back on more proc_id with different size_vecs..
for
// now use 1 element.

vec_int_pair v2;
v2.push_back(std::make_pair(proc_id, 0));
// ditto.. more proc_ids all zerod out.. for now use 1 element

int bytes_xferred = 0x100000;

{
v2[0].second = bytes_xferred;
if (v1[0].first == proc_id) // pure test purspose. so fix this so
the construct is true ..

{
if (v2[0].second == v1[0].second[0])
{
// I would like to erase
// v1[0].second[0] here...
// my intent is to ultimately erase all the size_vec elements
in vec_int_vec_pair
cout << " erasing " << endl;
//cout << v1[0].second.size(); // check to see if size is 3.
// revist this after
perusing text.. how to do this
// given vector pair
v2[0].second = 0;
}
}
}
return 0;
}

At issue:
My intent is to ultimately earse all elements in size_vec based on some
'condition'. For now I'm just trying to erase one element and could
use assistance erasing " v1[0].second[0] ".

Thanks in advance.

Dec 31 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11********************@g44g2000cwa.googlegroups.c om>,
"ma740988" <ma******@gmail.com> wrote:
// I would like to erase
// v1[0].second[0] here...
v1[0].second.erase(v1[0].second.begin());
// my intent is to ultimately erase all the size_vec elements
in vec_int_vec_pair
cout << " erasing " << endl;
//cout << v1[0].second.size(); // check to see if size is 3.
// revist this after
perusing text.. how to do this
// given vector pair

It might read better if you split it up a little, perhaps using a
reference:

{
VEC_INT& first_vec = v1[0].second;
if (v2[0].second == first_vec.front())
{
// I would like to erase
// v1[0].second[0] here...
first_vec.erase(first_vec.begin());
cout << " erasing " << endl;
//cout << v1[0].second.size(); // check to see if size is 3.
v2[0].second = 0;

The reference not only makes the code more readable, you might also get
slightly better code generation since you're saving a link to the vector
instead of continually recomputing it (although a good optimizer might
see that too).

-Howard
Dec 31 '05 #2

P: n/a
|| The reference not only makes the code more readable,
|| you might also get slightly better code generation since
|| you're saving a link to the vector instead of continually
|| recomputing it (although a good optimizer might see that too).

Howard, thanks.. I tend to make this a little harder than it is
sometimes... I'm _slowly_ working my way around the STL though.

One other question. Curiosity - more or less. We're given std::pair,
with which we could do all sorts of cool things with.
Why no:
std::triple <int, int, int> - or some such thing?

I have a case it'd be ideal if I could fit three elements in a vector
of 'triples' (used sparingly). i.e

processor id (unsigned int proc_id)
offset within processor (unsigned char* ptr_offset)
bytes transferred (unsigned int bytes_xferred)

I opted to use a map. Such that: map <int , vector<pair> >. The
strict weak ordering of the map makes it unfit for the job since I'm
not interested in sorting the Keys (proc_id). Of course an
alternative I'm wrestiling with is the use of a struct.

You see, I'm communicating with five processors on a card. The master
processor - in this case - needs to maintain information about the
other processors + itself. Simply put the master maintains a list of
all id's (to include it's own). The current offset within each
processor memory (since the master is responsible for doling out data
to the other processors) and the number of bytes transferred to each
processor.

Dec 31 '05 #3

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>,
"ma740988" <ma******@gmail.com> wrote:
Why no:
std::triple <int, int, int> - or some such thing?


The plain simple truth is that you're ahead of the standard. We're
trying to catch up, but standards are slow.

There is a first technical library report out that is experimenting with
something like:

#include <tuple>

int main()
{
std::tr1::tuple<int, int, int> tp;
}

It is only a TR (Technical Report), and not standard. Your vendor might
supply it as shown, or perhaps under:

#include <tr1/tuple>

or perhaps not at all.

You can read the paper at:

http://www.open-std.org/jtc1/sc22/wg...2005/n1836.pdf

(search for 6.1 tuple types)

It is my hope that std::tr1::tuple will be standardized in C++0X as
std::tuple.

In the mean-time you can work with:

http://www.boost.org/libs/tuple/doc/...ers_guide.html

-Howard
Jan 1 '06 #4

P: n/a
|| It is my hope that std::tr1::tuple will be standardized in C++0X
|| as std::tuple.

I see.

Last question - I think on this. Given

struct tp { int bytes_xferred; int offset; };
typedef std::pair<int, tp > int_tp_pair;
typedef std::vector< int_tp_pair > vec_int_tp_pair;

int main()
{
vec_int_tp_pair vp;
tp t;
t.offset = 50;
vp.push_back(std::make_pair(5, t));
t.offset = 100;
vp.push_back(std::make_pair(6, t));
vp.push_back(std::make_pair(7, t));
vp.push_back(std::make_pair(8, t));
}

Pair second is a struct. Does each push_back of type tp results in a
push back of the same 't' or a four separate copies of t?
Sample runs suggest 4 separate copies of t but I'm unsure if I have the
'wording' right.

Thanks for the guidance

Jan 1 '06 #5

P: n/a

ma740988 wrote in message

struct tp { int bytes_xferred; int offset; };
typedef std::pair<int, tp > int_tp_pair;
typedef std::vector< int_tp_pair > vec_int_tp_pair;


???
// ----------------------------------------------
#ifndef VECTTMP_H
#define VECTTMP_H
template <class T> class VecT{ // template basic 3D vector
public: // ------------------------------ public
VecT():x(0),y(0),z(0){}
VecT(T tx, T ty, T tz):x(tx),y(ty),z(tz){}
virtual ~VecT(){}
// ----------------------------------------
T x; T y; T z; // note: intentionally public
};
#endif //#ifndef VECTTMP_H
// ----------------------------------------------
std::vector<VecT<int> > IntVec(43);
for(size_t i(0); i < IntVec.size(); ++i){
IntVec.at( i ).x = i+1;
if( i < 7 ){ IntVec.at( i ).z = 50;}
else{ IntVec.at( i ).z = 100;}
} // for(i) // all .y == 0
// ----------------------------------------------
std::vector<VecT<double> > DbleVec(5, VecT<double>(1.5, 0.01, 7.7));
for(size_t i(0); i < DbleVec.size(); ++i){
cout <<"DbleVec.at(" << i << ") x="
<<DbleVec.at(i).x <<" y="<<DbleVec.at(i).y
<<" z="<<DbleVec.at(i).z <<std::endl;
}
//// DbleVec.at( /* [0-4] */ ) x=1.500000 y=0.010000 z=7.700000
// ----------------------------------------------

--
Bob R
POVrookie
Jan 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.