Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:53:35+00:00 2026-06-12T04:53:35+00:00

I am trying to use remove_copy_if to copy an iterable directly to stdout through

  • 0

I am trying to use remove_copy_if to copy an iterable directly to stdout through ostream_iterator. The remove_copy_if guarantees that the return value is an iterator to the end of the output range. This return value is useful in a way to determine the no of elements copied to the destination, by finding out the distance between the start of the output iterator and the return iterator. This makes sense when using a container but how to use the same functionality to determine the no of elements copied to the destination if its an ostream_iterator.

The following example should make more sense to what I might want to achieve

#include<vector>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iostream>
#include <time.h>
using namespace std;
int  main() {
    int rawdata[] = {1,2,3,4,5,6,7,8,9,10,11};
    vector<int> data(20);
    vector<int>::iterator curr = remove_copy_if(rawdata,rawdata + sizeof(rawdata)/sizeof(rawdata[0]),data.begin(),bind2nd(greater<int>(),10));
    wcout<<L"No of data copied = "<<curr - data.begin()<<endl;
    for(int i=0;i<10;i++) {
    int some_value = rand()%20 + 1;
    ostream_iterator<int> curr = remove_copy_if(data.begin(),data.end(),ostream_iterator<int>(cout),bind2nd(less<int>(),some_value));
    //if (curr - what???? > 0) 
        cout<<endl;
    }
    return 0;
    } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:53:36+00:00Added an answer on June 12, 2026 at 4:53 am

    I came across the following non-thread safe , not so elegant solution to make this work.

    #include<vector>
    #include<iterator>
    #include<algorithm>
    #include<functional>
    #include<iostream>
    #include <time.h>
    #include <algorithm>
    using namespace std;
    template<class _Fn1>
    class counter
        : public std::unary_function<typename _Fn1::argument_type, bool>
        {   
        public:
        explicit counter(const _Fn1& _Func)
            : _Functor(_Func)   {
            count = countTrue = countFalse = 0;
    
            }
    
        bool operator()(const typename _Fn1::argument_type& _Left) const
            {   
            count++;
            if (_Functor(_Left)) {
            countTrue++;
            return true;
            } else {
                countFalse++;
                return false;
            }
            }
        static int count, countTrue,countFalse;
        protected:
    
        _Fn1 _Functor;  // the functor to apply
        };
    template<class _Fn1>
    int counter<_Fn1>::count=0,counter<_Fn1>::countTrue=0,counter<_Fn1>::countFalse=0;
    int  main() {
        int rawdata[] = {1,2,3,4,5,6,7,8,9,10,11};
        vector<int> data(20);
        vector<int>::iterator curr = remove_copy_if(rawdata,rawdata + sizeof(rawdata)/sizeof(rawdata[0]),data.begin(),bind2nd(greater<int>(),10));
        wcout<<L"No of data copied = "<<curr - data.begin()<<endl;
        for(int i=0;i<10;i++) {
        int some_value = rand()%20 + 1;
        ostream_iterator<int> curr = remove_copy_if(data.begin(),data.end(),ostream_iterator<int>(cout),counter<binder2nd<less<int>>>(bind2nd(less<int>(),some_value)));
        if (counter<binder2nd<less<int>>>::countFalse) {
        cout<<endl<<"No of data printed = "<<counter<binder2nd<less<int>>>::countFalse<<endl;
        }
        }
        return 0;
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the some what same logic that is used in
I'm running into some bugs now that I'm trying to use the doubly linked
I am trying to write a Python script that can move and copy files
I have been trying to use array_unique to remove duplicates from the search results
I am trying to use MSBuild community tasks to remove the slash from the
I'm trying to use Unix cut to remove the first two fields per line.
I'm trying to use git on windows to clone a remote repository. I can
I am trying to use local port forwarding to access remote host over a
I am trying to use python's telnetlib module to get information from a remote
i'm trying use facebook API to upload photo in my fan page. I downloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.