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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:42:36+00:00 2026-05-27T19:42:36+00:00

I have a vector<string> vectorStrings with values: ta, bc, ac, st, cer, cda .

  • 0

I have a vector<string> vectorStrings with values: ta, bc, ac, st, cer, cda. I want to find the first occurrence of any of the strings in the vector in an input string.

e.g.

InputStr = "this certainly helps";

Of the given strings in the vector, I would want a way to say "cer" was the first occurrence at position 5.


int min = 9999999;
string first;

for(int i = 0; i < vectorStrings.size(); i++)
{
    int pos = InputStr.find(vectorStrings[i]);

    if(pos == string::npos)
        continue;

    if(pos < min)
    {
        min = pos;
        first = vectorStrings[i];
    }
}

// values of min and first gives which string occurred first
// and at the position of it in the input string

This implementation works, but I would want to know if there exists a more elegant way to do this with boost libraries or std library.

I am working on Windows and using Visual Studio 2010.

  • 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-27T19:42:36+00:00Added an answer on May 27, 2026 at 7:42 pm

    This is a MapReduce problem.

    First, you want to go from vector<string> to vector<int>, of their positions, which is a map, and then you want to reduce the values to one value by their minimum, which is a reduction. First, the map. This is std::transform.

    std::vector<std::string> stuff;
    std::string input;
    // fill stuff and input
    std::vector<int> positions;
    std::transform(
        stuff.begin(), 
        stuff.end(), 
        std::back_inserter(positions), 
        [&](std::string& stuff) {
            return input.find(stuff);
        }
    );
    

    Now we simply use std::min_element to get the smallest element, the reduce.

    auto iterator = std::min_element(positions.begin(), positions.end());
    int index = *iterator;
    

    To find the string that was found there, it’s a simple bit of iterator arithmetic:

    string found = stuff[iterator - positions.begin()];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have vector of strings and want to create a fixed with string out
I have a Vector < String >. Now i want to store those Strings
I have a string vector and i want to iterate through the vector and
I have a vector of strings example: dedf eedf fedf hedf I want to
I have a vector< vector<string> > and want to add a string to the
A have a vector of strings in c++: vector<string> myVect = {A, A, A,
I have a vector of strings: std::vector<std::string> fName which holds a list of file
I have a vector of strings. d <- c(Mon,Tues,Wednes,Thurs,Fri,Satur,Sun) for which I want to
I have a vector of pair like such: vector<pair<string,double>> revenue; I want to add
I have made a string vector vector<string> actor_; and then added elements in it

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.