Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 31st, 2005, 07:45 PM
ma740988
Guest
 
Posts: n/a
Default 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.

  #2  
Old December 31st, 2005, 08:15 PM
Howard Hinnant
Guest
 
Posts: n/a
Default Re: erasing element in a vector of pair ..

In article <1136057529.424512.4580@g44g2000cwa.googlegroups.c om>,
"ma740988" <ma740988@gmail.com> wrote:
[color=blue]
> // I would like to erase
> // v1[0].second[0] here...[/color]

v1[0].second.erase(v1[0].second.begin());
[color=blue]
> // 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[/color]


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
  #3  
Old December 31st, 2005, 09:55 PM
ma740988
Guest
 
Posts: n/a
Default Re: erasing element in a vector of pair ..

|| 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.

  #4  
Old January 1st, 2006, 04:15 AM
Howard Hinnant
Guest
 
Posts: n/a
Default Re: erasing element in a vector of pair ..

In article <1136065312.811794.82840@g43g2000cwa.googlegroups. com>,
"ma740988" <ma740988@gmail.com> wrote:
[color=blue]
> Why no:
> std::triple <int, int, int> - or some such thing?[/color]

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
  #5  
Old January 1st, 2006, 07:45 AM
ma740988
Guest
 
Posts: n/a
Default Re: erasing element in a vector of pair ..

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

  #6  
Old January 1st, 2006, 07:45 PM
BobR
Guest
 
Posts: n/a
Default Re: erasing element in a vector of pair ..


ma740988 wrote in message[color=blue]
>
>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;
>[/color]

???


// ----------------------------------------------
#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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles