473,472 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12000
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...
0
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,...
0
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,...
1
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...
0
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...
1
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...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.