Akash De Silva
Akash De Silva

Reputation: 43

find the longest word in a vector c++

I have a c++ vector which contains about 106 thousand words which are stored on vector<string>words I need to find the longest word in this vector and I also need to get the location at which the word is, for example (1,2,3) in my vector. I need this location because I have two other vectors which has the meaning and the type for the words. vector<string>definition, vector<string>type

My current code

this code is not working at all

copy_if(words.begin(), words.end(), back_inserter(length), [](const string& x) { return x.length() > 40; });// looks for words longer than 7 letters
   
    for (const string& s : length)
    {
        cout << "found!!" << endl;
        auto i = find(words.begin(), words.end(), s);//looks for the word in the words vector
        if (i != words.end())
        {
            auto pos = i - words.begin();
            //displays the word, type and the definition of the word that the user has entered
            cout << "Word      : " << words[pos] << '\n';
            cout << "Type      : " << definitions[pos] << '\n';
            cout << "Definition: " << types[pos] << '\n';
            cout << '\n';

        }
        else
            cout << "word not found" << endl;
    }

Upvotes: 0

Views: 5698

Answers (2)

Ted Lyngmo
Ted Lyngmo

Reputation: 117832

You could use the standard algorithm std::max_element to search through the vector<string>.

Example:

#include <algorithm> // max_element
#include <iostream>
#include <iterator>  // distance
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words{"a", "bb", "ccc"};

    auto it = std::max_element(words.begin(), words.end(),
                               [](const auto& a, const auto& b) {
                                   return a.size() < b.size();
                               });

    std::cout << "The longest word is " << *it << " at (zero-based) pos "
              << std::distance(words.begin(), it) << '\n';
}

Output:

The longest word is ccc at (zero-based) pos 2

Upvotes: 8

MikeCAT
MikeCAT

Reputation: 75062

I would prefer thinking simply: just check length of elements according to each indice and update information according to that.

std::vector<std::string> length;

// initialize the vector length

size_t max_length = 0; // the length of longest word(s)
std::vector<size_t> max_indice; // the indice of longest word(s)
for (size_t i = 0; i < length.size(); i++) {
    size_t this_len = length[i].length();
    if (this_len > max_length) {
        // new record
        max_length = this_len;
        max_indice.clear();
        max_indice.push_back(i);
    } else if (this_len == max_length) {
        // tie
        max_indice.push_back(i);
    }
}

for (size_t pos : max_indice) {
    cout << "Word      : " << words[pos] << '\n';
    cout << "Type      : " << definitions[pos] << '\n';
    cout << "Definition: " << types[pos] << '\n';
    cout << '\n';
}

Upvotes: 1

Related Questions