Python C++;搜索索引 #包括 #包括 #包括 #包括 使用名称空间std; get(数组[位置]); 位置++; } 数组[位置-1]='\0'; 对于(int i=0;数组[i]!='\0';i++) { 有很多算法,比如Knuth Morris Pratt或Rabin Karp。我认为wikipedia对它们的描述非常完美。但它们通常适合于一个字符串。对于多个字符串,最好的算法是从文本中构建后缀树并搜索树。

Python C++;搜索索引 #包括 #包括 #包括 #包括 使用名称空间std; get(数组[位置]); 位置++; } 数组[位置-1]='\0'; 对于(int i=0;数组[i]!='\0';i++) { 有很多算法,比如Knuth Morris Pratt或Rabin Karp。我认为wikipedia对它们的描述非常完美。但它们通常适合于一个字符串。对于多个字符串,最好的算法是从文本中构建后缀树并搜索树。,python,c++,arrays,string,Python,C++,Arrays,String,最简单的方法是使用标准库。目前搜索是线性的,但从c++17开始,如果性能是关键,该库将允许轻松的并行化 这里有一个只使用标准库的解决方案(需要c++11) 请注意从流中读取字符串的正确方法 #include <iostream> #include <string> #include <sstream> #include <fstream> using namespace std; infile.get(ar

最简单的方法是使用标准库。目前搜索是线性的,但从c++17开始,如果性能是关键,该库将允许轻松的并行化

这里有一个只使用标准库的解决方案(需要c++11)

请注意从流中读取字符串的正确方法

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;




                 infile.get(array[position]); 
                 position++;
            }
            array[position - 1] = '\0'; 
            for (int i = 0; array[i] != '\0'; i++)
            {
                cout << array[i];
            }

        }
    }
    cout << "Displaying Array..." << array << endl;

    //stringstream buffer;
    //buffer << in.rdbuf();
    //std::string test = buffer.str();
    //std::cout << test << std::endl << std::endl;
    //

    system("pause");
}

请正确使用stream.eof()-避免它-搜索Internet这
(infle.eof())
是错误的,如果文件包含某些内容,它将永远不会启动。@DimChtz请给出一些,如何修复it@kastozu为什么您的代码中有这么多
infle.eof()
?对于start,首先将
while(infle.eof())
更改为
while(!infle.eof())
@DimChtz不喜欢使用任何形式的while-eof。它从来都不正常工作。说真的。只是不要这样做。这个逻辑是这样的:“我的数据好吗?好吧,现在读一下。”想想看。在读取数据之前,你怎么知道数据是否良好?先读取,然后测试。接下来,测试eof是不够的。其他事情可能会出错,导致无限循环,因为流现在不可读,但不是文件的结尾,并且永远不会到达文件的结尾,因为流不可读。更多信息:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
#include <iomanip>

using search_term_vector = std::vector<std::string>;
using search_results = std::map<std::string, std::vector<std::size_t>>;

std::vector<std::string> load_search_terms()
{
    constexpr char search_term_file[] = "pig dog cat garbage";
    std::istringstream is(search_term_file);

    std::vector<std::string> search_terms;
    std::string term_buffer;
    while (is >> term_buffer) {
        search_terms.push_back(std::move(term_buffer));
    }
    return search_terms;
}

search_results search_space_for_terms(const std::string& search_space, const search_term_vector& terms)
{
    search_results results;

    for (auto const& term : terms)
    {
        auto& result = results[term];
        auto icurrent = std::begin(search_space);
        while (icurrent != std::end(search_space))
        {
            auto ipos = std::search(icurrent, std::end(search_space),
                                    std::begin(term), std::end(term));
            if (ipos != std::end(search_space))
            {
                result.push_back(std::distance(std::begin(search_space), ipos));
                std::advance(ipos, term.length());
            }
            icurrent = ipos;
        }
    }

    return results;
}

int main()
{
    using namespace std::string_literals;

    auto search_space = "tdogicatzhpigu"s;
    auto search_terms = load_search_terms();
    auto results = search_space_for_terms(search_space, search_terms);

    for (auto const& result_pair : results)
    {
        std::cout << std::quoted(result_pair.first) << " was found ";
        auto const& locations = result_pair.second;
        if (locations.empty())
        {
            std::cout << "nowhere" << std::endl;
        }
        else
        {
            auto sep = (locations.size() > 1) ? "at positions " : "at position ";
            for (auto pos : locations)
            {
                std::cout << sep << pos;
                sep = ", ";
            }
            std::cout << std::endl;
        }
    }

    return 0;
}
"cat" was found at position 5
"dog" was found at position 1
"garbage" was found nowhere
"pig" was found at position 10