473,388 Members | 1,326 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,388 software developers and data experts.

removing elements from vector<int> using <algorithm>

WANTED:

/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.

int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:

/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:

In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $

CODE:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );

/* I made this array behave like a container:
"ia_begin" is the pointer to 1st element of ia
"ia_end" is the pointer to one past the last element of ia
*/
int *ia_begin = ia;
int *ia_end = ia + ia_size;

std::vector<intivec;
std::list<int ilist;

/* copy elements from array to vector & list */
std::copy( ia_begin, ia_end, std::back_inserter( ivec ) );
std::copy( ia_begin, ia_end, std::back_inserter( ilist ) );
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );

/* print out the values */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int(std::cout, "\n" ) );

return 0;
}

I am just trying to use Lambda from Std. Lib. Why it is the problem ?

-- arnuld
http://lispmachine.wordpress.com

Oct 10 '07 #1
10 6040
"arnuld" <No****@NoPain.comwrote in message
news:pa****************************@NoPain.com...
WANTED:

/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.

int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:

/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:

In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $

CODE:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );

/* I made this array behave like a container:
"ia_begin" is the pointer to 1st element of ia
"ia_end" is the pointer to one past the last element of ia
*/
int *ia_begin = ia;
int *ia_end = ia + ia_size;

std::vector<intivec;
std::list<int ilist;

/* copy elements from array to vector & list */
std::copy( ia_begin, ia_end, std::back_inserter( ivec ) );
std::copy( ia_begin, ia_end, std::back_inserter( ilist ) );
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );

/* print out the values */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int(std::cout, "\n" ) );

return 0;
}

I am just trying to use Lambda from Std. Lib. Why it is the problem ?
You might want to re-read your homework assignment again. "... Use the
single iterator form of erase to remove the elements..." I don't believe
std::remove_if qualifies as erase. I believe the instructor may be looking
for ivec.erase( iterator ); but this is just how I read it.
Oct 10 '07 #2
On 2007-10-10 09:34, arnuld wrote:
WANTED:

/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.

int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:

/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:

In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $

CODE:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );

/* I made this array behave like a container:
"ia_begin" is the pointer to 1st element of ia
"ia_end" is the pointer to one past the last element of ia
*/
int *ia_begin = ia;
int *ia_end = ia + ia_size;

std::vector<intivec;
std::list<int ilist;

/* copy elements from array to vector & list */
std::copy( ia_begin, ia_end, std::back_inserter( ivec ) );
std::copy( ia_begin, ia_end, std::back_inserter( ilist ) );
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );

/* print out the values */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int(std::cout, "\n" ) );

return 0;
}

I am just trying to use Lambda from Std. Lib. Why it is the problem ?
First, there is not lambda in the standard library (at least not yet,
they might add it in the next version) and second, you are trying to use
the boos lambda, but have not included any boost headers.

By the way, the assignment wants you to use erase() (not sure if they
meant std::erase or std::vector<T>::erase(), I suspect the latter).

--
Erik Wikström
Oct 10 '07 #3
arnuld a écrit :
WANTED:

/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.

int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:

/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:

In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope /home/arnuld/programming/c++ $
[snip]
std::remove_if( ivec.begin(),
ivec.end(),
_1 % 2 == 0 );

[snip]

I am just trying to use Lambda from Std. Lib. Why it is the problem ?
There is no lambda in STL (for now).
Instead, you can use the std::modulus<functor together with
std::bind2nd().

Michael
Oct 10 '07 #4
On Wed, 10 Oct 2007 08:07:23 +0000, Erik Wikström wrote:
First, there is not lambda in the standard library (at least not yet,
they might add it in the next version) and second, you are trying to use
the boos lambda, but have not included any boost headers.
OK, I will not use BOOST for now.

By the way, the assignment wants you to use erase() (not sure if they
meant std::erase or std::vector<T>::erase(), I suspect the latter).
I tried it with vector's erase member function but that falls into the
infinite loop:
void rem_evens( std::vector<int>& ivec)
{
std::vector<int>::iterator begin = ivec.begin();
std::vector<int>::iterator end = ivec.end();

while( begin != end )
{
if( (*begin % 2) == 0 )
{
begin++ = ivec.erase( begin );
}
end = ivec.end();
}

}

int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
const size_t ia_size = sizeof( ia ) / sizeof( *ia );

int *ia_begin = ia;
int *ia_end = ia + ia_size;

std::vector<intivec;
std::list<int ilist;

/* copy elements from array to vector & list */
std::copy( ia_begin, ia_end, std::back_inserter( ivec ) );
std::copy( ia_begin, ia_end, std::back_inserter( ilist ) );

/* vector before values are removed */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int>( std::cout, "\n" ) );
rem_evens( ivec );

/* vector after even values are removed */
std::copy( ivec.begin(), ivec.end(),
std::ostream_iterator<int>( std::cout, "\n" ) );

return 0;
}


-- arnuld
http://lispmachine.wordpress.com

Oct 10 '07 #5
On Wed, 10 Oct 2007 02:38:10 -0700, James Kanze wrote:
I'm not sure what this statement is supposed to do, but it
looks very, very wrong (and may not compile with some
implementations).

Note that the statement has a very different meaning
depending on whether vector<>::iterator is a typedef for a
pointer, or is a class type. Since the code shouldn't
compile if it is a typedef for a pointer, I will assume that
it is a class type,
yes, it is a class because VECTOR is a class and erase is its memebr
function.

which means that you have the equivalent of:
begin.operator++( 0 ).operator=( ivec.erase( begin ) ) ;
I dont' know and don't understand what that

begin.operator++( 0 ).operator

means :-(
while ( begin != end ) {
if ( condition ) {
begin = vec.erase( begin ) ;
} else {
++ begin ;
}
}
}
You either increment, or you use the iterator returned by erase.
OK ,i got it but since we have removed the element, so the ivec.end()
iterator defined previously (before erasing) must invalidate but it does
not whereas insert() invalidates such things . I don't understand the
phenomenon.

Of course, the classical idiom would use remove here:

vec.erase(
remove_if( vec.begin(), vec.end(), condition ), vec.end() ) ;


Aha... that's good :-) but that's new to me. so this member function takes
2 arguments:

ivec.erase( algorithm, end_iterator )

it is mysterious, why it takes end_iterator here. I need to look up the
that ERASE member function in Stroustrup .


-- arnuld
http://lispmachine.wordpress.com

Oct 10 '07 #6
Michael DOUBEZ wrote:
arnuld a écrit :
>WANTED:
/* C++ Primer - 4/e
*
* Exercise: 9.26
* STATEMENT
* Using the following definition of ia, copy ia into a vector and
into a list. Use the single iterator form of erase to remove the
elements with odd values from your list * and the even values from your
vector.
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
*/
WHAT I GET:

/home/arnuld/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra
ex_09.26.cpp ex_09.26.cpp:
In function 'int main()': ex_09.26.cpp:43:
error: '_1' was not declared in this scope
/home/arnuld/programming/c++ $ [snip]
std::remove_if( ivec.begin(), ivec.end(),
_1 % 2 == 0 );
[snip]

I am just trying to use Lambda from Std. Lib. Why it is the problem ?

There is no lambda in STL (for now).
Instead, you can use the std::modulus<functor together with
std::bind2nd().
std::not2 is also needed, the Predicate looks like:
bind2nd(not2(modulus<int>()), 2))
Oct 10 '07 #7
arnuld wrote:
>On Wed, 10 Oct 2007 02:38:10 -0700, James Kanze wrote:
>I'm not sure what this statement is supposed to do, but it
looks very, very wrong (and may not compile with some
implementations).

Note that the statement has a very different meaning
depending on whether vector<>::iterator is a typedef for a
pointer, or is a class type. Since the code shouldn't
compile if it is a typedef for a pointer, I will assume that
it is a class type,

yes, it is a class because VECTOR is a class and erase is its memebr
function.

>which means that you have the equivalent of:
> begin.operator++( 0 ).operator=( ivec.erase( begin ) ) ;

I dont' know and don't understand what that

begin.operator++( 0 ).operator

means :-(
> while ( begin != end ) {
if ( condition ) {
begin = vec.erase( begin ) ;
} else {
++ begin ;
}
}
}
You either increment, or you use the iterator returned by erase.

OK ,i got it but since we have removed the element, so the ivec.end()
iterator defined previously (before erasing) must invalidate but it does
not whereas insert() invalidates such things . I don't understand the
phenomenon.

>Of course, the classical idiom would use remove here:

vec.erase(
remove_if( vec.begin(), vec.end(), condition ), vec.end() ) ;

Aha... that's good :-) but that's new to me. so this member function takes
2 arguments:

ivec.erase( algorithm, end_iterator )

it is mysterious, why it takes end_iterator here. I need to look up the
that ERASE member function in Stroustrup .
template < class ForwardIterator, class Predicate >
ForwardIterator remove_if ( ForwardIterator first,
ForwardIterator last,
Predicate pred )
{
ForwardIterator result = first;
for ( ; first != last; ++first)
if (!pred(*first)) *result++ = *first;
return result;
}

actually remove_if does NOT remove any thing, it just move the matched
items forward to overwrite those are not.
std::vector<int>::iterator pos
= remove_if(ivec..begin(), ivec.end(),
std::bind2nd(std::not2(std::modulus<int>()), 2));

now, pos points to the first item that's not matched

iterator vector::erase (iterator first, iterator last);

ivec.erase(pos, ivec.end());

now it's clear that, vector::erase erases a iterator range


Oct 10 '07 #8
arnuld wrote:
>On Wed, 10 Oct 2007 02:38:10 -0700, James Kanze wrote:
>I'm not sure what this statement is supposed to do, but it
looks very, very wrong (and may not compile with some
implementations).

Note that the statement has a very different meaning
depending on whether vector<>::iterator is a typedef for a
pointer, or is a class type. Since the code shouldn't
compile if it is a typedef for a pointer, I will assume that
it is a class type,

yes, it is a class because VECTOR is a class and erase is its memebr
function.

>which means that you have the equivalent of:
> begin.operator++( 0 ).operator=( ivec.erase( begin ) ) ;

I dont' know and don't understand what that

begin.operator++( 0 ).operator

means :-(

means that you can't increment rvalue return by a function.

int f() { return 10; }
++f(); //modifying an rvalue is illformed

But VC has an extension to compile the code. you can turn it off by /Za
Oct 10 '07 #9
arnuld wrote:
On Wed, 10 Oct 2007 02:38:10 -0700, James Kanze wrote:
I'm not sure what this statement is supposed to do, but it
looks very, very wrong (and may not compile with some
implementations).
Note that the statement has a very different meaning
depending on whether vector<>::iterator is a typedef for a
pointer, or is a class type. Since the code shouldn't
compile if it is a typedef for a pointer, I will assume that
it is a class type,
yes, it is a class because VECTOR is a class and erase is its memebr
function.
vector<>::iterator can be a class type, but it can also be a
typedef to a pointer. In most of the early implementations, it
was just a typedef to a pointer.
which means that you have the equivalent of:
begin.operator++( 0 ).operator=( ivec.erase( begin ) ) ;
I dont' know and don't understand what that
begin.operator++( 0 ).operator
means :-(
It's the function which overloads of the ++ operator. For
overloaded operators, the name of the function which overloads
the operator is "operator <op>", where <opis the operator
being overloaded.

++ and -- are a bit special, as well, since there are two
versions, one for prefix, and one for postfix. For whatever
reasons, the postfix version takes an additional int argument;
the compiler passes it 0 when it is being called using the
normal operator syntax.
while ( begin != end ) {
if ( condition ) {
begin = vec.erase( begin ) ;
} else {
++ begin ;
}
}
You either increment, or you use the iterator returned by erase.
OK ,i got it but since we have removed the element, so the ivec.end()
iterator defined previously (before erasing) must invalidate but it does
not whereas insert() invalidates such things. I don't understand the
phenomenon.
Erase on a vector invalidates all iterators which designate the
element, or follow it. But you're right that that includes the
end iterator, so the loop should read:

while ( begin != vec.end() ) ...

If you're erasing in the loop, you can't cache the end()
iterator.
Of course, the classical idiom would use remove here:
vec.erase(
remove_if( vec.begin(), vec.end(), condition ), vec.end() ) ;
Aha... that's good :-) but that's new to me. so this member function takes
2 arguments:
ivec.erase( algorithm, end_iterator )
vector::erase() has two overloads: one takes a single iterator,
and removes just that element; the other takes two iterators,
which define a sequence of elements to be removed.

The algorithm remove_if returns an iterator. You should read
the specifications of the function.
it is mysterious, why it takes end_iterator here. I need to
look up the that ERASE member function in Stroustrup .
You need to look up all of the overloads to erase, and you need
to look up the algorithm remove/remove_if: because of the fact
that (like all of the algorithms), they take iterators, and not
containers, they can't actually remove anything. So all they do
is regroup the elements which aren't removed at the front of the
sequence, and return an iterator to the first element which
follows. This is used, with the end iterator, as an argument to
the erase function, which does the actual removal.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 10 '07 #10
On Oct 10, 2:09 pm, Barry <dhb2...@gmail.comwrote:
arnuld wrote:
On Wed, 10 Oct 2007 02:38:10 -0700, James Kanze wrote:
I'm not sure what this statement is supposed to do, but it
looks very, very wrong (and may not compile with some
implementations).
Note that the statement has a very different meaning
depending on whether vector<>::iterator is a typedef for a
pointer, or is a class type. Since the code shouldn't
compile if it is a typedef for a pointer, I will assume that
it is a class type,
yes, it is a class because VECTOR is a class and erase is its memebr
function.
which means that you have the equivalent of:
begin.operator++( 0 ).operator=( ivec.erase( begin ) ) ;
I dont' know and don't understand what that
begin.operator++( 0 ).operator
means :-(
means that you can't increment rvalue return by a function.
Not at all. First, of course, from a formal point of view, you
can't "increment" a class type at all; all you can do is call a
function on it (or use it as an argument to a function). When
the compiler sees a ++ applied to a class type; the compiler
tries to find a function named operator++ which can be called.

And of course, if the return value of a function is a class
type for which operator++ is defined, you can call operator++ on
it:-). But that's not what's happening here.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 10 '07 #11

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

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
3
by: silverburgh.meryl | last post by:
Hi, Is there a STL algorithm to compare if 2 vector<inthave same values? Thank you.
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.