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

vector assign() trouble

I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4. Aren't
these code segments equivalent?

Jan 8 '07 #1
11 2400
forgot the definition of

#define BE(v) v.begin(), v.end()

Jan 8 '07 #2
na***********@gmail.com wrote:
I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4.
Aren't these code segments equivalent?
Of course not. If the vector is empty, pushing back into it fills
it with those elements. Assigning does not introduce new elements
into the vector, but instead changes the values of the existing
ones. The latter variant is actually equivalent to

{ size_t i = 0;
for (it = s.begin(); it != s.end(); ++it)
v[i++] = *it;
}

You need to precede your call to 'assign' with 'resize':

v.resize(s.size());
v.assign(BE(s));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '07 #3

na***********@gmail.com skrev:
I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4. Aren't
these code segments equivalent?
As Victor already pointed out, assign assigns and does not create new
elements. But I do not see the need for the function in the first
place: why not simply write
std::vector<Tv(s.begin(),s.end());

? This will almost certainly be optimal code.

/Peter

Jan 9 '07 #4
peter koch wrote:
na***********@gmail.com skrev:
>I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4.
Aren't these code segments equivalent?

As Victor already pointed out, assign assigns and does not create new
elements. But I do not see the need for the function in the first
place: why not simply write
std::vector<Tv(s.begin(),s.end());

? This will almost certainly be optimal code.
I would guess that the function is for repeated use with already
created vector (no need to reconstruct it). The following does the
same thing

existingvector.swap(std::vector(s.begin(), s.end()));

but less readable than

set2vector(s, existingvector);

(arguably).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '07 #5
Victor Bazarov wrote:
na***********@gmail.com wrote:
>I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4.
Aren't these code segments equivalent?

Of course not. If the vector is empty, pushing back into it fills
it with those elements. Assigning does not introduce new elements
into the vector, but instead changes the values of the existing
ones. The latter variant is actually equivalent to

{ size_t i = 0;
for (it = s.begin(); it != s.end(); ++it)
v[i++] = *it;
}

You need to precede your call to 'assign' with 'resize':

v.resize(s.size());
v.assign(BE(s));

V
I am confused. I found in 23.2.4.1

template<class InputIterator>
void assign(InputIterator _First, InputIterator _Last);

Effects:

erase(begin(), end());
insert(begin(), first, last);
Jan 9 '07 #6
"peter koch" <pe***************@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>
As Victor already pointed out, assign assigns and does not create new
elements.
I'm sorry but that's just nonsense. The assign function does the same as
it's corresponding constructor in vector, namelijk reconstructing the vector
with the range of elements you've provided. And as the OP already pointed
out, the standard clearly says so:

template<class InputIterator>
void assign(InputIterator _First, InputIterator _Last);

Effects:

erase(begin(), end());
insert(begin(), first, last);
Of course this still doesn't make the two functions equivalent: the first
pushes all the elements to the end of the vector - it doesn't clear it
first. I think the segfault is caused by something else that just happened
to be triggered by the minor change in code. If both the set and the vector
are valid, v.assign(s.begin(), s.end()) won't segfault on you unless either
your T is conceptually flawed or your heap is corrupted (generally speaking
of course, there could be tons of other totally unrelated issues).

- Sylvester
Jan 9 '07 #7
"Sylvester Hesp" <s.****@oisyn.nlwrote in message
news:45*********************@news.xs4all.nl...
namelijk
I'm sorry my Dutch became intertwined with the words. I meant "namely" ;)
Jan 9 '07 #8
On Mon, 8 Jan 2007 20:10:54 -0500, "Victor Bazarov" wrote:
>I would guess that the function is for repeated use with already
created vector (no need to reconstruct it). The following does the
same thing

existingvector.swap(std::vector(s.begin(), s.end()));
You cannot use a temporary with vector::swap().

Best wishes,
Roland Pibinger
Jan 9 '07 #9
Volker Wippel wrote:
Victor Bazarov wrote:
>na***********@gmail.com wrote:
>>I am trying to replace

template < class T void
set2vector (const set < T &s, vector < T &v)
{
typename set < T >::iterator it;
for (it = s.begin (); it != s.end (); it++) {
v.push_back (*it);
}
}

with

template < class T void
set2vector (const set < T &s, vector < T &v)
{
v.assign(BE(s));
}

but I get a segmentation fault. I am using g++ on Fedora core 4.
Aren't these code segments equivalent?

Of course not. If the vector is empty, pushing back into it fills
it with those elements. Assigning does not introduce new elements
into the vector, but instead changes the values of the existing
ones. The latter variant is actually equivalent to

{ size_t i = 0;
for (it = s.begin(); it != s.end(); ++it)
v[i++] = *it;
}

You need to precede your call to 'assign' with 'resize':

v.resize(s.size());
v.assign(BE(s));

V

I am confused. I found in 23.2.4.1

template<class InputIterator>
void assign(InputIterator _First, InputIterator _Last);

Effects:

erase(begin(), end());
insert(begin(), first, last);
You're right. I messed up. In my defence I didn't have the code
to work with. See FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '07 #10
Roland Pibinger wrote:
On Mon, 8 Jan 2007 20:10:54 -0500, "Victor Bazarov" wrote:
>I would guess that the function is for repeated use with already
created vector (no need to reconstruct it). The following does the
same thing

existingvector.swap(std::vector(s.begin(), s.end()));

You cannot use a temporary with vector::swap().
Right. It ought to be the other way around:

std::vector(s.begin(), s.end()).swap(existingvector);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '07 #11

Sylvester Hesp skrev:
"peter koch" <pe***************@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...

As Victor already pointed out, assign assigns and does not create new
elements.

I'm sorry but that's just nonsense. The assign function does the same as
it's corresponding constructor in vector, namelijk reconstructing the vector
with the range of elements you've provided.
[snip]

Right. One of the (few) times that Victor was wrong and my appeal to
authority became to strong;-)

/Peter

Jan 13 '07 #12

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

Similar topics

1
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
4
by: ahrimen | last post by:
Hi, First I'll state my over all goal = a text based game with several rooms that have several exits as my first real program that I've done without the help of a book. I can make a normal...
14
by: Tarun | last post by:
Hello, I am facing problem sometimes while I am trying to do push_back on a vector. Currently I am doing resize of the vector increasing the size by one and then push_back and seems like the...
4
by: Chris Roth | last post by:
vector<doublev1(5,1); vector<doublev2; v2 = v1; // 1 v2.assign(v1.begin(),v1.end()); // 2 Are 1 and 2 the same, or are their subtle differences between them. Which is preferable, if either? ...
14
by: meisterbartsch | last post by:
Hi, I want to assign predefined vallues to a vector, like: std::vector<doubletr; tr={3.36,2.09,1.47,1.1,0.87,0.72}; how do i do this? Am I able to assign values without using .push_back();...
29
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would...
5
by: John Doe | last post by:
Hi, I have a static array of struct defined like this : CViewMgr::ViewInfo g_ViewInfo = { { EMainView, ECreateOnce, IDR_MAINFRAME, RUNTIME_CLASS(CMainView), NULL,0, 0 }, {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.