Created
April 3, 2025 21:49
-
-
Save jweinst1/95e5bd1c002251c1b1bbe130ffe0144a to your computer and use it in GitHub Desktop.
events search in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <filesystem> | |
#include <vector> | |
#include <cstdio> | |
#include <cstdlib> | |
struct EventPos { | |
size_t ind = 0; | |
size_t size = 0; | |
void clear() { | |
ind = 0; | |
size = 0; | |
} | |
}; | |
static void generateEvents(const std::string& data, char lineSep, std::vector<EventPos>& events) { | |
bool currentMatch = false; | |
EventPos cur; | |
for (size_t i = 0; i < data.size(); ++i) | |
{ | |
if (currentMatch) { | |
if (data[i] == lineSep) { | |
events.push_back(cur); | |
cur.clear(); | |
currentMatch = false; | |
} else { | |
cur.size++; | |
} | |
} else { | |
if (data[i] != lineSep) { | |
cur.ind = i; | |
currentMatch = true; | |
} | |
} | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
const std::string foo = "hello \n sir \n"; | |
std::vector<EventPos> pairs; | |
generateEvents(foo, '\n', pairs); | |
std::printf("%zu\n", pairs.size()); | |
const auto found = foo.find("hello"); | |
const auto found2 = foo.find("hello", found + 5); | |
std::printf("%zu %zu\n", found, found2); | |
std::printf("%s\n", found2 == foo.npos ? "not found" : "found"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment