小编dan*_*ftk的帖子

C++ 模板模板参数类型推导

我有代码可以在遍历字符串容器时找到并打印出模式的匹配项。打印在模板化的函数foo中执行

编码

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <tuple>
#include <utility>

template<typename Iterator, template<typename> class Container>
void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings)
{
    for (auto const &finding : findings)
    {
        std::cout << "pos = " << std::distance(first, finding.first) << " ";
        std::copy(finding.first, finding.second, std::ostream_iterator<char>(std::cout));
        std::cout << '\n';
    }
}

int main()
{
    std::vector<std::string> strs = { "hello, world", "world my world", "world, it is me" };
    std::string const pattern = "world";
    for …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-templates language-lawyer c++11

10
推荐指数
1
解决办法
584
查看次数