Home > other >  Can you compare a string and a string from a vector?
Can you compare a string and a string from a vector?

Time:12-02

I'm new to C . Whenever I try to compare a string and a string from a vector, it gives me an error. I included two examples below. Why does this happen?

#include <iostream>
#include <vector>
#include <string>

int main() {
  std::string vowels = ("aeiou");
  std::string whale_talk = "turpentine and turtles";
  std::vector<std::string> result;
  for (int i = 0; i < whale_talk.size(); i  ) {
    for (int x = 0; x < vowels.size(); x  ) {
      if (whale_talk[i] == vowels[x]) {
        std::cout << whale_talk[i];
        result.push_back(whale_talk[i]);
        // I'm aware I'm not comparing two vectors, I added this to show that most interaction with strings will also result in an error
      }
    }
  }
}
#include <string>
#include <iostream>
#include <vector>

int main() {
  std::vector <std::string> string_vector;
  std::string string = "Hello";
  std::cout << "What do you want today?";
  string_vector = {"pickles"};
  if (string[2] == string_vector[0]) {
    std::cout << "No pickles today";
  }
  else {
    std::cout << "We only have pickles";
  }
}

I tried adding and removing #include <string>, but that did not help. I also tried putting strings inside the vector before comparing it to a string.

CodePudding user response:

#include <iostream>
#include <vector>
#include <string>

int main() {
    std::string vowels = ("aeiou");
    std::string whale_talk = "turpentine and turtles";
    std::string result; 
    //std::vector<std::string> result;
    for (unsigned int i = 0; i < whale_talk.size(); i  ) {
        for (unsigned int x = 0; x < vowels.size(); x  ) {
            if (whale_talk[i] == vowels[x]) {
                std::cout << whale_talk[i];
                result.push_back(whale_talk[i]);
                // I'm aware I'm not comparing two vectors, I added this to show that most interaction with strings will also result in an error
            }
        }
    }
}

CodePudding user response:

The error that you are seeing is occurring because you are trying to compare a character from a string (which is a single character) with a string from a vector (which is a sequence of characters). In C , you cannot compare these two types directly, because they are not the same type.

To fix this, you can use the std::string::at() method to retrieve a single character from a string, and then compare that character with a character from the string in the vector. Here is an example of how you might do this:

#include <vector>
#include <string>

int main() {
  std::string vowels = ("aeiou");
  std::string whale_talk = "turpentine and turtles";
  std::vector<std::string> result;
  for (int i = 0; i < whale_talk.size(); i  ) {
    for (int x = 0; x < vowels.size(); x  ) {
      // Use the at() method to retrieve a single character from the string
      if (whale_talk.at(i) == vowels.at(x)) {
        std::cout << whale_talk.at(i);
        result.push_back(whale_talk.at(i));
      }
    }
  }
}

In this example, we use the at() method to retrieve a single character from each string, and then compare those characters using the == operator.

For the second part of your code, you can fix the error by using the std::string::at() method to retrieve a single character from the string, and then compare that character with a single character from the string in the vector. Here is an example of how you might do this:

#include <string>
#include <iostream>
#include <vector>
int main() {
  std::vector <std::string> string_vector;
  std::string string = "Hello";
  std::cout << "What do you want today?";
  string_vector = {"pickles"};
  // Use the at() method to retrieve a single character from the string
  if (string.at(2) == string_vector.at(0).at(0)) {
    std::cout << "No pickles today";
  }
  else {
    std::cout << "We only have pickles";
  }
}

In this example, we use the at() method to retrieve a single character from the string and from the string in the vector, and then compare those characters using the == operator.

I hope this helps!

  • Related