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 7637103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:51:27+00:00 2026-05-31T07:51:27+00:00

I have following code: #include <iostream> #include <fstream> #include <algorithm> #include <iterator> #include <vector>

  • 0

I have following code:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <functional>

using namespace std;

typedef istream_iterator<string> is_it;
typedef vector<string>::iterator v_str_it;

int main()
{
    int i = 4;
    ifstream ifstr("1.txt");
    is_it ifstrm(ifstr);
    is_it eof;
    vector<string> v_str(ifstrm, eof);
    v_str_it vsit = v_str.begin();
    while( (vsit = find_if(vsit, v_str.end(),
        bind2nd(equal_to<string>(), i ))) != v_str.end())
    {
        cout << *vsit << endl;
        ++vsit;
    }
    return 0;
}

As far as I understand in find_if(vsit, v_str.end(), bind2nd(equal_to<string>(), i ) i should use const char like "sometext" instead of int i. But how can i find words with length equal to 4 e.g. ? I’m confused and need some advice.

  • 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-05-31T07:51:28+00:00Added an answer on May 31, 2026 at 7:51 am

    find_if will only return the first item in the sequence that satisfies the predicate.

    For this you really want a lambda and if you are using C++11. This will look something like:

    [](std::string const& x) { return x.size() == i; }
    

    (Not sure of the exact syntax).

    To create a “functor” which is the simplest here you might do:

    struct CompareStringLength
    {
       int len_;
       explicit CompareStringLength( int len ) : len_(len)
       {
       }
    
       bool operator()(std::string const& str ) const
       {
          return str.size() == len_;
       }
    };
    

    Within your vector you would now use std::find_if( v.begin(), v.end(), CompareStringLength(i) );

    to get the first element. To find all of them there is no std::copy_if to copy them into another vector so you’d actually have to create a different predicate that returns the opposite and use remove_copy_if which does exist or write your own copy_if algorithm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
I have the following code compiled by gcc: #include <iostream> using namespace std; class
i have following code #include <iostream> using namespace std; int main(int argc,char arg[]){ int
i have following code #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class
i have following code #include <iostream> using namespace std; int main(){ int i; char
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following code: #include <iostream> #include boost/unordered_map.hpp using namespace std; using namespace
I have the following code: #include stdafx.h #include <iostream> #include <conio.h> using namespace std;
i have following code for copying content of vector into file #include<iterator> #include<algorithm> #include<fstream>

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.