Home > other >  How to find out if a std::vector in a string in cpp?
How to find out if a std::vector in a string in cpp?

Time:07-25

I have a string like ...hello...world..., and I want to check if it is in some strings,so I split it by ... and save it in a <vector> string,the problem is how to check both of item in input_str sequentially?

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string input_str1 = "Good morning, hello, a beautiful world";//true
    string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
    //item = "...hello...world..."
    vector<string> item;
    item.push_back("hello");
    item.push_back("world");
    bool status = false;
    for (auto sub_item : item)
    {
        cout << sub_item << ' ';
        if (input_str2.find(sub_item) != string::npos) //change str1 to str2 
            status = true;
        else
        {
            status = false;
            break;
        }
    }
     cout << status;
}  

Checking input_str1 works fine, but for input_str2 the output should be 0 because the two words appear not in the right order. My code prints 1 for both.

CodePudding user response:

I'd suggest Regular Expressions if it's ok with your assignment. It'd look like this:

#include <iostream>
#include <vector>
#include <regex>

using namespace std;

int main()
{
    string input_str1 = "Good morning, hello, a beautiful world";//true
    string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true

    //item = "...hello...world..."
    const std::regex rxItems{"hello.*world"};
    const bool status = std::regex_search(input_str2, rxItems);

    cout << status;
}

This code searches for the strings "hello" and "world" in sequence, but accepts any characters (or even none) in between.

CodePudding user response:

std::string::find returns the position where the string was found and there is an overload that takes the position to start searching as paramter:

#include <iostream>
#include <vector>

using namespace std;

bool check(const std::string& input_str2) {
    vector<string> item;
    item.push_back("hello");
    item.push_back("world");
    size_t index = 0;
    for (auto sub_item : item)
    {
        cout << sub_item << ' ';
        index = input_str2.find(sub_item,index);
        if (index == string::npos) {
            return false;
        }
    }
    return true;
}


int main()
{
    string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
    //item = "...hello...world..."
     cout << check(input_str2);
}  

Better start with status = true because if items is empty then finding 0 items is always successful. Then there is no need to assign true when a word is found and when placed in a function the variable can be removed alltogether.

CodePudding user response:

Initialize bool status = true; and remove status = true;. You start with the assumption that the string contains all words. Then, you iterate over the list of words and check each word. If a word doesn't exist, set status = false;.

Store the position of the last result and start the next search at that position.

#include <iostream>
#include <vector>

int main()
{
    std::string input_str1 = "Good morning, hello, a beautiful world";//true
    std::string input_str2 = "what a wonderful world, hello!"; //false, but I only can return true
    //item = "...hello...world..."
    std::vector<std::string> item;
    item.push_back("hello");
    item.push_back("world");
    bool status = true;
    std::string::size_type pos = 0;
    for (auto sub_item : item)
    {
        std::cout << sub_item << ' ';
        pos = input_str2.find(sub_item, pos);
        if (pos == std::string::npos)
        {
            status = false;
            break;
        }
    }
    std::cout << status;
}

Output

hello world 0
  • Related