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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:21:58+00:00 2026-06-12T23:21:58+00:00

I have two vectors containing strings. I want to compare each string of vector1

  • 0

I have two vectors containing strings. I want to compare each string of vector1 with each string of vector2 and check how many words are the same in both strings. The code I have only works if the two strings are perfectly similar :

Compare::Compare(vector<string> text1, vector<string> text2, int ratio)
{
    text1Size_ = text1.size();
    text2Size_ = text2.size();

    if(text1Size_ > text2Size_)
    {
        totalWords_ = text1Size_;
    }
    else
    {
        totalWords_ = text2Size_;
    }

    it = text1.begin();

    for(int i = 0; i < text1Size_; i++)
    {
        it2 = text2.begin();

        for(int i = 0; i < text2Size_; i++)
        {
            if(*it == *it2)
            {
                cout << "Perfect match";
            }
            it2++;
        }
        it++;
    }
}

I need to return each similar string if they have at least the ratio of similar words.

Is there a easier way than to parse each string, put each word in an array and compare them?

-EDIT-

By word I mean a written word like “bird”. I’ll give an example.

Let says I only have one string per vector and I need a 70% ratio of similarities:

string1 : The blue bird.
string2 : The bird.

What I want to do is to check if there is at least 60% of the written words that match in both sentences.

Here I have “The” and “Bird” that match. So I have 2/3 similar words (66.666%). So theses strings will be accepted.

-EDIT 2-

I don’t think I can use “.compare()” here since it will check each character and not each written word…

  • 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-12T23:21:59+00:00Added an answer on June 12, 2026 at 11:21 pm

    Use a string stream to split a string into words:

    #include <sstream>
    
    bool is_similar(string str1, string str2)
    {
        vector<string> words1, words2;
        string temp;
    
        // Convert the first string to a list of words
        std::stringstream stringstream1(str1);
        while (stringstream1 >> temp)
            words1.push_back(temp);
    
        // Convert the second string to a list of words
        std::stringstream stringstream2(str2);
        while (stringstream2 >> temp)
            words2.push_back(temp);
    
        int num_of_identical_words = 0;
        // Now, use the code you already have to count identical words
        ...
    
        double ratio = (double)num_of_identical_words / words2.size();
        return ratio > 0.6;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two vectors and want to compare their contents (strings) but this does
I'd like to do the following. but don't know how: //have two vectors: vector1
Elementwise multiplication of two vectors is no problem if they both have the same
I have two vectors of floats, x and y, and I want to compute
I have two vectors typedef std::vector<std::string> messages; typedef std::vector<std::string> addMessage; messages st; addMessage additionlMsgs;
I have two vectors of integers, and for each element of the second vector
I have two floating-point number vectors which contain the same values up to a
I have two vectors of dates A and B. I would like for each
Operation A I have N vectors, each containing certain number of unique 3D points.
I have two vectors of floats and i want them to become one vector

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.