473,221 Members | 2,321 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,221 software developers and data experts.

erasing element in a vector of pair ..


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
5 11955
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
|| 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
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
|| 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

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

Similar topics

5
by: Owen Brydon | last post by:
Hi, Is the following code legal? g++ 3.2 barfs on it, although it seems fine to me. #include <map> using std::map; int main()
8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
4
by: John Black | last post by:
Hi, I just found out that in looping the vector, you can not push_back() to the vector even in some "safe" cases. Here is the code snippet, vector<pair<UINT32, UINT32> >::iterator itr; for (itr...
18
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
18
by: pmatos | last post by:
Hi, If I have set<unsigned int> s; will *(s.begin()) give me the minimum element of the set? If not, how can I retrieve it? Cheers, Paulo Matos
11
by: eeykay | last post by:
Hello, I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the...
8
by: Jim Langston | last post by:
There's the thing about iterating though a map or vector when you may delete one of the elements, where you simply assign the iterator to the map.erase() statement or increment it if you don't. ...
1
by: wolverine | last post by:
Hi, I have read that to erase an element from a vector with reverse_iterator we have to use -- vector.erase( (++reverseItr).base()) -- But assuming i have to delete the first element of the...
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.