Home > Software design >  Segmentation fault looking for longest strings in a vector
Segmentation fault looking for longest strings in a vector

Time:01-08

vector<string> solution(vector<string> ia) {
  int maxi = -1;
  int size = ia.size();
  vector<string> iasol;
  for (int i = 0; i < size; i  ) {
    int m = ia[i].length();
    cout << m << " " << maxi << endl;
    if (m > maxi) {
      maxi = m;
    }
  }
  for (int i = 0; i < size; i  ) {
    int m = ia[i].length();
    if (m == maxi) {
      iasol[i] = ia[i];
      cout << iasol[i];
    }
  }
  return iasol;
}

this is the problem if it helps

Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be

solution(inputArray) = ["aba", "vcd", "aba"].

CodePudding user response:

Since I found the readability of the source code you developed low, I developed a new solution for this problem.

#include <iostream>
#include <vector>

using namespace std;

/* Returns the size of the vector with the maximum length. */
size_t getMaximumSize(vector<string> input);

/* Returns a vector container based on the string length. */
vector<string> getElementBySize(vector<string> input, size_t size);

/* Prints the vector container. */
void print(vector<string> input);

int main()
{
    vector<string> input{"aba", "aa", "ad", "vcd", "aba"};
    print(getElementBySize(input, getMaximumSize(input)));
    return 0;
}

vector<string> getElementBySize(vector<string> input, size_t size)
{
    vector<string> result;
    
    for(int i = 0 ; i < input.size();   i)
        if(input[i].size() == size)
            result.push_back(input[i]);
            
    return result;
}

size_t getMaximumSize(vector<string> input)
{
    size_t maximumNumberOfCharacters = input[0].size();

    for(int i = 0 ; i < input.size() - 1 ;   i)
        if(input[i].size() < input[i 1].size())
            maximumNumberOfCharacters = input[i 1].size();
    
    return maximumNumberOfCharacters;
}

void print(vector<string> input)
{
    for(string i : input)
        cout << i << ' ';
}

This program produces the following output:

aba vcd aba

CodePudding user response:

The problem is that iasol is initially empty so indexing it with any value of i will be out of bounds. std::vector grows when you add elements to it. Instead of iasol[i] = ia[i]; in your second loop, do iasol.push_back(ia[i]); to add it to the result. live example

I'd also recommend using ranged-for rather than index-based to make this clearer.

std::vector<std::string> solution(const std::vector<std::string>& input) {
  std::size_t max_size{};
  for (const auto& s : input) {
    auto sz = s.size();
    if (sz > max_size) {
      max_size = sz;
    }
  }
  
  std::vector<std::string> longest_strings;
  for (const auto& s : input) {
    if (s.size() == max_size) {
      longest_strings.push_back(s);
    }
  }
  return longest_strings;
}

live link

  •  Tags:  
  • Related