#calculating index positions based on match data

3 messages · Page 1 of 1 (latest)

blissful peakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

vivid mortar
#

@wet rivet What's your question

eternal crane
#
#include <iostream>
#include <vector>
#include <string>

// Helper function to find all occurrences of a pattern in a string
std::vector<std::pair<int, int>> find_pattern(const std::string& buffer, const std::string& pattern) {
    std::vector<std::pair<int, int>> matches;
    size_t pos = buffer.find(pattern, 0);
    while (pos != std::string::npos) {
        matches.push_back(std::make_pair(pos, pos + pattern.size() - 1));
        pos = buffer.find(pattern, pos + 1);
    }
    return matches;
}

int main() {
    std::string buffer = "21212";
    std::string pattern = "12";
    
    auto matches = find_pattern(buffer, pattern);
    
    std::cout << "Buffer: " << buffer << "\n";
    std::cout << "Match found for pattern \"" << pattern << "\" in the following buffer:\n";
    std::cout << buffer << "\n\n";
    std::cout << "Matches size: " << matches.size() << "\n";

    for (int i = 0; i < matches.size(); i++) {
        std::cout << "buffer[begin_index /* " << i*2 << " */] = " << matches[i].first + 1 << "\n";
        std::cout << "buffer[end_index   /* " << i*2 + 1 << " */] = " << matches[i].second + 1 << "\n";
    }

    return 0;
}