I looked this up on multiple forum pages, not just stack overflow, and have tried many solutions from the 'Check if a string contains a string in C ' post, along with others, and tried almost every single solution posed but none of them seem to work for me? I tried the vector[i].find(std::string2)
along with if(strstr(s1.c_str(),s2.c_str())) {cout << " S1 Contains S2";}
along with
std::string in = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
std::string needle = "pisci";
auto it = std::search(in.begin(), in.end(),
std::boyer_moore_searcher(
needle.begin(), needle.end()));
if(it != in.end())
std::cout << "The string " << needle << " found at offset "
<< it - in.begin() << '\n';
else
std::cout << "The string " << needle << " not found\n";
and more solutions, (adapted to my code), but none have worked. The only one I didn't try was std::string.contain()
but that's because visual studios (2019 v.142 -if that helps) [C standard language I'm using, Preview - Features from the Latest C Working Draft (std:c latest),] wouldn't recognize the function -because it wouldn't recognize the larger library for some reason. I even tried reversing the two variables in case I was mixing the two up, and was looking for the larger variable in the smaller one.
I'm rather new at C so I'm not great at problem-solving with stuff like this, so forgive my ignorance if you would.
Does the problem arise because what I'm looking for is in a vector? I created a vector <string> names = {"Andrew John", "John Doe",};
with names in it, and am trying to 'peer' into it and find keywords, but again, nothing works. Is there a special function to call when looking for something in a vector? Any help would be very much appreciated!
CodePudding user response:
How do you find a string/char inside another string that's in a vector in C ?
If the std::vector<std::string>
isn't sorted, I would use std::find_if
.
Example:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
// A vector with some strings:
std::vector<std::string> vec{
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"},
{"Foo bar"}};
std::string needle = "pisci";
size_t pos;
// find and save position in the found string
auto it = std::find_if(vec.begin(), vec.end(),
[&needle,&pos](const std::string& str) {
return (pos = str.find(needle)) != std::string::npos;
});
if(it != vec.end()) {
std::cout << "Found needle at position " << pos
<< " in string\n" << *it << '\n';
}
}
CodePudding user response:
Your sample code does not match your textual description. You are not searching a vector of strings, but a string. In fact, it seems to be based on the example in CPPreference.
Found needle at position 43 in string
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
And it works when I try it.
It might be overkill for your search, since you are only searching the string once. You are probably better off with a simple find
.
What this doesn't show at all is that you want it in a loop, with in
replaced by each value in the vector of strings. That's a simple for
loop:
for (const auto& in : myvector) { ...