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 10 5881
"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.
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
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
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
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
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))
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;
...
|
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...
|
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()
|
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++...
|
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...
|
by: silverburgh.meryl |
last post by:
Hi,
Is there a STL algorithm to compare if 2 vector<inthave same values?
Thank you.
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |