The function: replace_copy_if produces no output in my program

In this program the function: replace_copy works fine, but replace_copy_if gives no output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  //STL  Function remove, remove_if, removecopy, and remove_copy_if
#include<iostream>
#include<cctype>
#include<algorithm>
#include<iterator>
#include<vector>

using namespace std;

int main() {
	
	
	int num;
	
	ostream_iterator<int> screen(cout, " ");
	
		int list[10] = {12, 34, 56, 21, 34, 78, 34, 55, 12, 25};
	vector<int> intList(list, list + 10);                                
	
	cout << endl << endl;
	copy(intList.begin(), intList.end(), screen);
	vector<int> temp(10);                                                	

	cout <<endl << endl;
	replace_copy(intList.begin(), intList.end(), temp.begin(), 34, 0);   

	copy(temp.begin(), temp.end(), screen);
	bool lessThanEqualTo50(int num);                                      
	{
		return (num <= 50);
	}
	replace_copy_if(intList.begin(), intList.end(), 
					temp.begin(), lessThanEqualTo50, 50);                 cout<< endl;
	copy(temp.begin(),  temp.end(), screen);	
}
You cannot define a function inside another function.*

It appears to compile because you have a semicolon separating the function header from the function body, so the compiler is reading that as a prototype, followed by code wrapped in a local context.

Get rid of that semicolon at the end of line 28 and move lines 28-31 to somewhere above main().

You also have an extra, unused variable on line 13.


----
* But you can define function objects...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
  std::ostream_iterator <int> screen( std::cout, " " );

  std::vector <int> list {12, 34, 56, 21, 34, 78, 34, 55, 12, 25};
  copy( list.begin(), list.end(), screen );
  std::cout << "\n";

  std::vector <int> temp( list.size() );

  std::replace_copy( list.begin(), list.end(), temp.begin(), 34, 0 );
  copy( temp.begin(), temp.end(), screen );
  std::cout << "\n";

  std::replace_copy_if( list.begin(), list.end(), temp.begin(), 
    []( int x ) -> int { return x <= 50; },
    50 );
  copy( temp.begin(), temp.end(), screen );
  std::cout << "\n";
}

Line 21 is a lambda: special syntax that creates a function object you can use inline. It works just like a normal function.

Hope this helps.
Last edited on
Your line 28 declares a function. It has nothing to do with lines 29-31.

Declaring a function inside another function is legal, but rarely done, except in mistake. (Seek "C++ most vexing parse".)

If you would not have semicolon at the end of line 28 then lines 28-31 would implement a function. Implementing a named function inside another function's scope is not legal.

Move lines 28-31 out of main, into line 7. Remove the unnecessary line 13 too.


Note: since C++11 it has been possible to implement unnamed lambda-functions inside other functions.
1
2
int list[10] = {12, 34, 56, 21, 34, 78, 34, 55, 12, 25};
vector<int> intList(list, list + 10);        
There is a easier way:

1
2
3
// std::initializer_list - since C++11
vector<int> intList = { 12, 34, 56, 21, 34, 78, 34, 55, 12, 25 }; 
// http://www.cplusplus.com/reference/initializer_list/initializer_list/ 
Thank you very much to all of you. I did not realize that the function bool was inside another function.
[]( int x ) -> int { return x <= 50; },
this should technically return bool, not int
OP: you can initialize the std::ostream_iterator<int> object directly within the call to std::replace_copy_if:
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector <int> myVec {12, 34, 56, 21, 34, 78, 34, 55, 12, 25};//avoid variable names like list

    std::replace_copy_if(myVec.cbegin(), myVec.cend(), std::ostream_iterator<int>(std::cout, " "),
                         [](const int x){return x <= 50;}, 0);
}
Topic archived. No new replies allowed.