473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2439
forgot the definition of

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

Jan 8 '07 #2
na***********@g mail.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***********@g mail.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***********@g mail.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::vecto r(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***********@g mail.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(InputIte rator _First, InputIterator _Last);

Effects:

erase(begin(), end());
insert(begin(), first, last);
Jan 9 '07 #6
"peter koch" <pe************ ***@gmail.comwr ote in message
news:11******** *************@1 1g2000cwr.googl egroups.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(InputIte rator _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.begi n(), 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.n lwrote in message
news:45******** *************@n ews.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::vecto r(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***********@g mail.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(InputIte rator _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

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

Similar topics

1
1821
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
17
3361
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
10
4843
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
2395
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 vector object out of a custom class, I can even use a single object of a class that has vector data members, but If my vector object of a custom class has a vector data member my program crashs not when I instaitant it, but if I try calling any of the...
14
9787
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 code is working fine. Is it a better idea to do resize befoire calling push_back? Regards, Tarun
4
6953
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? And yes, I know I could use the construction vector<doublev2(v1), but I'm giving an example above.
14
6460
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
3437
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 make for much more readable code that is faster to write in some situations. I've not seen this feature documented anywhere
5
4159
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 }, { EWelcomeView, ECreateAndDestroy, IDR_MENU_OKCANCEL, RUNTIME_CLASS(CWelcomeView), NULL,0, 0 },
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
2
3607
muto222
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.