寻找 C++ 中搜索和替换的圣杯

2024-02-21

最近,我正在寻找一种替换字符串中标记的方法,这本质上是查找和替换(但至少还有一种解决问题的方法),看起来像是相当平庸的任务。我已经提出了几种可能的实现,但从性能的角度来看,它们都不能令人满意。最好的成绩是每次迭代约 50us。这种情况很理想,字符串的大小永远不会增长,最初我省略了不区分大小写的要求
这是代码Coliru http://coliru.stacked-crooked.com/a/485aaa6ec86efccc

我的机器的结果:
Boost.Spirit 符号结果:3421?=3421
100000 次循环耗时 6060ms。
博耶-摩尔结果:3421?=3421
100000 个周期耗时 5959ms。
博耶摩尔医院结果:3421?=3421
100000 个周期耗时 5008ms。
高德纳·莫里斯·普拉特结果:3421?=3421
100000 个周期花费了 12451ms。
Naive STL 搜索和替换结果:3421?=3421
100000 个周期花费了 5532ms。
提升replace_all结果:3421?=3421
100000 次循环耗时 4860ms。

那么问题来了,这么简单的任务为什么要花这么长时间呢?人们可以说,好吧,简单的任务,继续并更好地实施它。但现实情况是,15 年前的 MFC 原生实现执行任务的速度要快几个数量级:

CString FillTokenParams(const CString& input, const std::unordered_map<std::string, std::string>& tokens)
{
    CString tmpInput = input;
    for(const auto& token : tokens)
    {
        int pos = 0;
        while(pos != -1)
        {
            pos = tmpInput.Find(token.first.c_str(), pos);
            if(pos != -1)
            {
                int tokenLength = token.first.size();
                tmpInput.Delete(pos, tokenLength);
                tmpInput.Insert(pos, token.second.c_str());
                pos += 1;
            }
        }
    }

    return tmpInput;
}

result:
MFC 原生搜索和替换结果:3421?=3421
100000 次循环耗时 516ms。
为什么这个笨拙的代码比现代 C++ 性能更好???为什么其他实现如此缓慢?我错过了一些基本的东西吗?

EDIT001:我已经投资了这个问题,代码是profiled并进行三次检查。你可能对此不满意,但 std::string::replace 并不需要时间。在任何 STL 实现中,搜索花费了大部分时间,boostspirit 在分配 tst(我猜是评估树中的测试节点)上浪费了时间。我不希望有人在函数中指出“这是你的问题”,然后问题就消失了。问题是 MFC 如何能够以 10 倍的速度完成同样的工作。

EDIT002:刚刚深入研究 Find 的 MFC 实现并编写了一个模仿 MFC 实现的函数

namespace mfc
{
std::string::size_type Find(const std::string& input, const std::string& subString, std::string::size_type start)
{
    if(subString.empty())
    {
        return std::string::npos;
    }

    if(start < 0 || start > input.size())
    {
        return std::string::npos;
    }

    auto found = strstr(input.c_str() + start, subString.c_str());
    return ((found == nullptr) ? std::string::npos : std::string::size_type(found - input.c_str()));
}
}

std::string MFCMimicking(const std::string& input, const std::unordered_map<std::string, std::string>& tokens)
{
    auto tmpInput = input;
    for(const auto& token : tokens)
    {
        auto pos = 0;
        while(pos != std::string::npos)
        {
            pos = mfc::Find(tmpInput, token.first, pos);
            if(pos != std::string::npos)
            {
                auto tokenLength = token.first.size();
                tmpInput.replace(pos, tokenLength, token.second.c_str());
                pos += 1;
            }
        }
    }

    return tmpInput;
}

结果:
MFC模仿展开结果:3421?=3421
100000 个周期耗时 411ms。
意思是4us。每次通话,击败那个 Cstrstr

EDIT003:使用 -Ox 编译并运行


MFC模仿展开结果:3421?=3421
100000 次循环耗时 660ms。
微量燃料电池 天真的搜索和替换结果:3421?=3421
100000 个周期耗时 856ms。
手动展开结果:3421?=3421
100000 次循环需要 1995 毫秒。
博耶-摩尔 结果:3421?=3421
100000 个周期花费了 6911ms。
博耶摩尔医院 结果:3421?=3421
100000 次循环耗时 5670ms。
高德纳·莫里斯·普拉特 结果:3421?=3421
100000 个周期耗时 13825ms。
朴素的 STL 搜索和 替换结果:3421?=3421
100000 次循环耗时 9531ms。
促进 全部替换结果:3421?=3421
100000 个周期耗时 8996ms。


使用 -O2 运行(如原始测量)但 10k 个周期


MFC模仿展开结果:3421?=3421
10000 个周期 104 毫秒。
MFC 原生搜索和替换结果:3421?=3421
10000 周期耗时 105 毫秒。
手动展开结果:3421?=3421
10000 周期耗时 356 毫秒。
博耶-摩尔结果:3421?=3421
10000次循环 花了 1355 毫秒。
博耶摩尔医院结果:3421?=3421
10000 周期耗时 1101 毫秒。
高德纳·莫里斯·普拉特结果:3421?=3421
10000 个周期花费了 1973 毫秒。
Naive STL 搜索和替换结果: 3421?=3421
10000 个周期花费了 923 毫秒。
提升replace_all 结果:3421?=3421
10000 个周期需要 880ms。


所以,我对 Qi 版本有一些观察。

还创建了X3版本。

最后,编写了一个手动扩展函数,击败了所有其他候选者(我希望它比 MFC 更快,因为它不关心重复删除/插入)。

如果需要,请跳至基准图表。

关于 Qi 版本

  1. 是的,符号表受到基于节点的容器的局部性问题的影响。它们可能不是您可以在此处使用的最佳匹配。
  2. 无需在每个循环中重建符号:
  3. 扫描直到下一个字符,而不是按字符跳过非符号:

    +(bsq::char_ - symbols)
    
inline std::string spirit_qi(const std::string& input, bsq::symbols<char, std::string> const& symbols)
{
    std::string retVal;
    retVal.reserve(input.size() * 2);

    auto beg = input.cbegin();
    auto end = input.cend();

    if(!bsq::parse(beg, end, *(symbols | +(bsq::char_ - symbols)), retVal))
        retVal = input;

    return retVal;
}

这已经快得多了。但:

手动循环

在这个简单的例子中,为什么不手动进行解析呢?

inline std::string manual_expand(const std::string& input, TokenMap const& tokens)
{
    std::ostringstream builder;
    auto expand = [&](auto const& key) {
        auto match = tokens.find(key);
        if (match == tokens.end())
            builder << "$(" << key << ")";
        else
            builder << match->second;
    };

    builder.str().reserve(input.size()*2);

    builder.str("");
    std::ostreambuf_iterator<char> out(builder);

    for(auto f(input.begin()), l(input.end()); f != l;) {
        switch(*f) {
            case '$' : {
                    if (++f==l || *f!='(') {
                        *out++ = '$';
                        break;
                    }
                    else {
                        auto s = ++f;
                        size_t n = 0;

                        while (f!=l && *f != ')')
                            ++f, ++n;

                        // key is [s,f] now
                        expand(std::string(&*s, &*s+n));

                        if (f!=l)
                            ++f; // skip '}'
                    }
                }
            default:
                *out++ = *f++;
        }
    }
    return builder.str();
}

这在我的机器上的性能要优越得多。

其他想法

您可以查看 Boost Spirit Lex,可能带有静态生成的标记表:http://www.boost.org/doc/libs/1_60_0/libs/spirit/doc/html/spirit/lex/abstracts/lexer_static_model.html http://www.boost.org/doc/libs/1_60_0/libs/spirit/doc/html/spirit/lex/abstracts/lexer_static_model.html。不过我并不是特别喜欢 Lex。

比较:

See the 互动图表 http://stackoverflow-sehe.s3.amazonaws.com/474bf790-9b9f-44a1-969d-e64d24fc30c9/stats.html

那是用Nonius https://github.com/rmartinho/nonius用于基准统计。

完整基准代码:http://paste.ubuntu.com/14133072/ http://paste.ubuntu.com/14133072/

#include <boost/container/flat_map.hpp>

#define USE_X3
#ifdef USE_X3
#   include <boost/spirit/home/x3.hpp>
#else
#   include <boost/spirit/include/qi.hpp>
#endif

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/searching/boyer_moore.hpp>
#include <boost/algorithm/searching/boyer_moore_horspool.hpp>
#include <boost/algorithm/searching/knuth_morris_pratt.hpp>
#include <string>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <nonius/benchmark.h++>
#include <nonius/main.h++>

using TokenMap = boost::container::flat_map<std::string, std::string>;

#ifdef USE_X3
    namespace x3  = boost::spirit::x3;

    struct append {
        std::string& out;
        void do_append(char const ch) const                       { out += ch;                      } 
        void do_append(std::string const& s)  const               { out += s;                       } 
        template<typename It>
        void do_append(boost::iterator_range<It> const& r)  const { out.append(r.begin(), r.end()); } 
        template<typename Ctx>
        void operator()(Ctx& ctx) const                           { do_append(_attr(ctx));          } 
    };

    inline std::string spirit_x3(const std::string& input, x3::symbols<char const*> const& symbols)
    {
        std::string retVal;
        retVal.reserve(input.size() * 2);
        append appender { retVal };

        auto beg = input.cbegin();
        auto end = input.cend();

        auto rule = *(symbols[appender] | x3::char_ [appender]);

        if(!x3::parse(beg, end, rule))
            retVal = input;

        return retVal;
    }
#else
    namespace bsq = boost::spirit::qi;

    inline std::string spirit_qi_old(const std::string& input, TokenMap const& tokens)
    {
        std::string retVal;
        retVal.reserve(input.size() * 2);
        bsq::symbols<char const, char const*> symbols;
        for(const auto& token : tokens) {
            symbols.add(token.first.c_str(), token.second.c_str());
        }

        auto beg = input.cbegin();
        auto end = input.cend();

        if(!bsq::parse(beg, end, *(symbols | bsq::char_), retVal))
            retVal = input;

        return retVal;
    }

    inline std::string spirit_qi(const std::string& input, bsq::symbols<char, std::string> const& symbols)
    {
        std::string retVal;
        retVal.reserve(input.size() * 2);

        auto beg = input.cbegin();
        auto end = input.cend();

        if(!bsq::parse(beg, end, *(symbols | +(bsq::char_ - symbols)), retVal))
            retVal = input;

        return retVal;
    }
#endif

inline std::string manual_expand(const std::string& input, TokenMap const& tokens) {
    std::ostringstream builder;
    auto expand = [&](auto const& key) {
        auto match = tokens.find(key);

        if (match == tokens.end())
            builder << "$(" << key << ")";
        else
            builder << match->second;
    };

    builder.str().reserve(input.size()*2);
    std::ostreambuf_iterator<char> out(builder);

    for(auto f(input.begin()), l(input.end()); f != l;) {
        switch(*f) {
            case '$' : {
                    if (++f==l || *f!='(') {
                        *out++ = '$';
                        break;
                    }
                    else {
                        auto s = ++f;
                        size_t n = 0;

                        while (f!=l && *f != ')')
                            ++f, ++n;

                        // key is [s,f] now
                        expand(std::string(&*s, &*s+n));

                        if (f!=l)
                            ++f; // skip '}'
                    }
                }
            default:
                *out++ = *f++;
        }
    }
    return builder.str();
}

inline std::string boost_replace_all(const std::string& input, TokenMap const& tokens)
{
    std::string retVal(input);
    retVal.reserve(input.size() * 2);

    for(const auto& token : tokens)
    {
        boost::replace_all(retVal, token.first, token.second);
    }
    return retVal;
}

inline void naive_stl(std::string& input, TokenMap const& tokens)
{
    input.reserve(input.size() * 2);
    for(const auto& token : tokens)
    {
        auto next = std::search(input.cbegin(), input.cend(), token.first.begin(), token.first.end());
        while(next != input.cend())
        {
            input.replace(next, next + token.first.size(), token.second);
            next = std::search(input.cbegin(), input.cend(), token.first.begin(), token.first.end());
        }
    }
}

inline void boyer_more(std::string& input, TokenMap const& tokens)
{
    input.reserve(input.size() * 2);
    for(const auto& token : tokens)
    {
        auto next =
            boost::algorithm::boyer_moore_search(input.cbegin(), input.cend(), token.first.begin(), token.first.end());
        while(next != input.cend())
        {
            input.replace(next, next + token.first.size(), token.second);
            next = boost::algorithm::boyer_moore_search(input.cbegin(), input.cend(), token.first.begin(),
                                                        token.first.end());
        }
    }
}

inline void bmh_search(std::string& input, TokenMap const& tokens)
{
    input.reserve(input.size() * 2);
    for(const auto& token : tokens)
    {
        auto next = boost::algorithm::boyer_moore_horspool_search(input.cbegin(), input.cend(), token.first.begin(),
                                                                  token.first.end());
        while(next != input.cend())
        {
            input.replace(next, next + token.first.size(), token.second);
            next = boost::algorithm::boyer_moore_search(input.cbegin(), input.cend(), token.first.begin(),
                                                        token.first.end());
        }
    }
}

inline void kmp_search(std::string& input, TokenMap const& tokens)
{
    input.reserve(input.size() * 2);
    for(const auto& token : tokens)
    {
        auto next = boost::algorithm::knuth_morris_pratt_search(input.cbegin(), input.cend(), token.first.begin(),
                                                                token.first.end());
        while(next != input.cend())
        {
            input.replace(next, next + token.first.size(), token.second);
            next = boost::algorithm::boyer_moore_search(input.cbegin(), input.cend(), token.first.begin(),
                                                        token.first.end());
        }
    }
}

namespace testdata {
    std::string const expected =
        "Five and Seven said nothing, but looked at Two. Two began in a low voice, 'Why the fact is, you see, Miss, "
        "this here ought to have been a red rose-tree, and we put a white one in by mistake; and if the Queen was to "
        "find it out, we should all have our heads cut off, you know. So you see, Miss, we're doing our best, afore "
        "she comes, to—' At this moment Five, who had been anxiously looking across the garden, called out 'The Queen! "
        "The Queen!' and the three gardeners instantly threw themselves flat upon their faces. There was a sound of "
        "many footsteps, and Alice looked round, eager to see the Queen.First came ten soldiers carrying clubs; these "
        "were all shaped like the three gardeners, oblong and flat, with their hands and feet at the corners: next the "
        "ten courtiers; these were ornamented all over with diamonds, and walked two and two, as the soldiers did. "
        "After these came the royal children; there were ten of them, and the little dears came jumping merrily along "
        "hand in hand, in couples: they were all ornamented with hearts. Next came the guests, mostly Kings and "
        "Queens, and among them Alice recognised the White Rabbit: it was talking in a hurried nervous manner, smiling "
        "at everything that was said, and went by without noticing her. Then followed the Knave of Hearts, carrying "
        "the King's crown on a crimson velvet cushion; and, last of all this grand procession, came THE KING AND QUEEN "
        "OF HEARTS.Alice was rather doubtful whether she ought not to lie down on her face like the three gardeners, "
        "but she could not remember ever having heard of such a rule at processions; 'and besides, what would be the "
        "use of a procession,' thought she, 'if people had all to lie down upon their faces, so that they couldn't see "
        "it?' So she stood still where she was, and waited.When the procession came opposite to Alice, they all "
        "stopped and looked at her, and the Queen said severely 'Who is this?' She said it to the Knave of Hearts, who "
        "only bowed and smiled in reply.'Idiot!' said the Queen, tossing her head impatiently; and, turning to Alice, "
        "she went on, 'What's your name, child?''My name is Alice, so please your Majesty,' said Alice very politely; "
        "but she added, to herself, 'Why, they're only a pack of cards, after all. I needn't be afraid of them!''And "
        "who are these?' said the Queen, pointing to the three gardeners who were lying round the rosetree; for, you "
        "see, as they were lying on their faces, and the pattern on their backs was the same as the rest of the pack, "
        "she could not tell whether they were gardeners, or soldiers, or courtiers, or three of her own children.'How "
        "should I know?' said Alice, surprised at her own courage. 'It's no business of mine.'The Queen turned crimson "
        "with fury, and, after glaring at her for a moment like a wild beast, screamed 'Off with her head! "
        "Off—''Nonsense!' said Alice, very loudly and decidedly, and the Queen was silent.The King laid his hand upon "
        "her arm, and timidly said 'Consider, my dear: she is only a child!'The Queen turned angrily away from him, "
        "and said to the Knave 'Turn them over!'The Knave did so, very carefully, with one foot.'Get up!' said the "
        "Queen, in a shrill, loud voice, and the three gardeners instantly jumped up, and began bowing to the King, "
        "the Queen, the royal children, and everybody else.'Leave off that!' screamed the Queen. 'You make me giddy.' "
        "And then, turning to the rose-tree, she went on, 'What have you been doing here?'";
    std::string const inputWithtokens =
        "Five and Seven said nothing, but looked at $(Two). $(Two) began in a low voice, 'Why the fact is, you see, "
        "Miss, "
        "this here ought to have been a red rose-tree, and we put a white one in by mistake; and if the Queen was to "
        "find it out, we should all have our $(heads) cut off, you know. So you see, Miss, we're doing our best, afore "
        "she comes, to—' At this moment Five, who had been anxiously looking across the garden, called out 'The Queen! "
        "The Queen!' and the three gardeners instantly threw themselves flat upon their faces. There was a sound of "
        "many footsteps, and Alice looked round, eager to see the $(Queen).First came ten soldiers carrying clubs; "
        "these "
        "were all shaped like the three gardeners, oblong and flat, with their hands and feet at the corners: next the "
        "ten courtiers; these were ornamented all over with $(diamonds), and walked two and two, as the soldiers did. "
        "After these came the royal children; there were ten of them, and the little dears came jumping merrily along "
        "hand in hand, in couples: they were all ornamented with hearts. Next came the guests, mostly Kings and "
        "Queens, and among them Alice recognised the White Rabbit: it was talking in a hurried nervous manner, smiling "
        "at everything that was said, and went by without noticing her. Then followed the Knave of Hearts, carrying "
        "the King's crown on a crimson velvet cushion; and, last of all this grand procession, came THE KING AND QUEEN "
        "OF HEARTS.Alice was rather doubtful whether she ought not to lie down on her face like the three gardeners, "
        "but she could not remember ever having heard of such a rule at processions; 'and besides, what would be the "
        "use of a procession,' thought she, 'if people had all to lie down upon their faces, so that they couldn't see "
        "it?' So she stood still where she was, and waited.When the procession came opposite to Alice, they all "
        "stopped and looked at her, and the $(Queen) said severely 'Who is this?' She said it to the Knave of Hearts, "
        "who "
        "only bowed and smiled in reply.'Idiot!' said the Queen, tossing her head impatiently; and, turning to Alice, "
        "she went on, 'What's your name, child?''My name is Alice, so please your Majesty,' said Alice very politely; "
        "but she added, to herself, 'Why, they're only a pack of cards, after all. I needn't be afraid of them!''And "
        "who are these?' said the $(Queen), pointing to the three gardeners who were lying round the rosetree; for, "
        "you "
        "see, as they were lying on their faces, and the $(pattern) on their backs was the same as the rest of the "
        "pack, "
        "she could not tell whether they were gardeners, or soldiers, or courtiers, or three of her own children.'How "
        "should I know?' said Alice, surprised at her own courage. 'It's no business of mine.'The Queen turned crimson "
        "with fury, and, after glaring at her for a moment like a wild beast, screamed 'Off with her head! "
        "Off—''Nonsense!' said $(Alice), very loudly and decidedly, and the Queen was silent.The $(King) laid his hand "
        "upon "
        "her arm, and timidly said 'Consider, my dear: she is only a child!'The $(Queen) turned angrily away from him, "
        "and said to the $(Knave) 'Turn them over!'The $(Knave) did so, very carefully, with one foot.'Get up!' said "
        "the "
        "Queen, in a shrill, loud voice, and the three gardeners instantly jumped up, and began bowing to the King, "
        "the Queen, the royal children, and everybody else.'Leave off that!' screamed the Queen. 'You make me giddy.' "
        "And then, turning to the rose-tree, she went on, 'What have you been doing here?'";

    static TokenMap const raw_tokens {
        {"Two", "Two"},           {"heads", "heads"},
        {"diamonds", "diamonds"}, {"Queen", "Queen"},
        {"pattern", "pattern"},   {"Alice", "Alice"},
        {"King", "King"},         {"Knave", "Knave"},
        {"Why", "Why"},           {"glaring", "glaring"},
        {"name", "name"},         {"know", "know"},
        {"Idiot", "Idiot"},       {"children", "children"},
        {"Nonsense", "Nonsense"}, {"procession", "procession"},
    };

    static TokenMap const tokens {
        {"$(Two)", "Two"},           {"$(heads)", "heads"},
        {"$(diamonds)", "diamonds"}, {"$(Queen)", "Queen"},
        {"$(pattern)", "pattern"},   {"$(Alice)", "Alice"},
        {"$(King)", "King"},         {"$(Knave)", "Knave"},
        {"$(Why)", "Why"},           {"$(glaring)", "glaring"},
        {"$(name)", "name"},         {"$(know)", "know"},
        {"$(Idiot)", "Idiot"},       {"$(children)", "children"},
        {"$(Nonsense)", "Nonsense"}, {"$(procession)", "procession"},
    };

}

NONIUS_BENCHMARK("manual_expand", [](nonius::chronometer cm)     {
    std::string const tmp = testdata::inputWithtokens;
    auto& tokens = testdata::raw_tokens;

    std::string result;
    cm.measure([&](int) {
        result = manual_expand(tmp, tokens);
    });
    assert(result == testdata::expected);
})

#ifdef USE_X3
NONIUS_BENCHMARK("spirit_x3", [](nonius::chronometer cm) {
    auto const symbols = [&] {
        x3::symbols<char const*> symbols;
        for(const auto& token : testdata::tokens) {
            symbols.add(token.first.c_str(), token.second.c_str());
        }
        return symbols;
    }();

    std::string result;
    cm.measure([&](int) {
            result = spirit_x3(testdata::inputWithtokens, symbols);
        });
    //std::cout << "====\n" << result << "\n====\n";
    assert(testdata::expected == result);
})
#else
NONIUS_BENCHMARK("spirit_qi", [](nonius::chronometer cm) {
    auto const symbols = [&] {
        bsq::symbols<char, std::string> symbols;
        for(const auto& token : testdata::tokens) {
            symbols.add(token.first.c_str(), token.second.c_str());
        }
        return symbols;
    }();

    std::string result;
    cm.measure([&](int) {
            result = spirit_qi(testdata::inputWithtokens, symbols);
        });
    assert(testdata::expected == result);
})

NONIUS_BENCHMARK("spirit_qi_old", [](nonius::chronometer cm) {
    std::string result;
    cm.measure([&](int) {
            result = spirit_qi_old(testdata::inputWithtokens, testdata::tokens);
        });
    assert(testdata::expected == result);
})
#endif

NONIUS_BENCHMARK("boyer_more", [](nonius::chronometer cm) {
    cm.measure([&](int) {
        std::string tmp = testdata::inputWithtokens;
        boyer_more(tmp, testdata::tokens);
        assert(tmp == testdata::expected);
    });
})

NONIUS_BENCHMARK("bmh_search", [](nonius::chronometer cm) {
    cm.measure([&](int) {
        std::string tmp = testdata::inputWithtokens;
        bmh_search(tmp, testdata::tokens);
        assert(tmp == testdata::expected);
    });
})

NONIUS_BENCHMARK("kmp_search", [](nonius::chronometer cm) {
    cm.measure([&](int) {
        std::string tmp = testdata::inputWithtokens;
        kmp_search(tmp, testdata::tokens);
        assert(tmp == testdata::expected);
    });
})

NONIUS_BENCHMARK("naive_stl", [](nonius::chronometer cm) {
    cm.measure([&](int) {
            std::string tmp = testdata::inputWithtokens;
            naive_stl(tmp, testdata::tokens);
            assert(tmp == testdata::expected);
        });
})

NONIUS_BENCHMARK("boost_replace_all", [](nonius::chronometer cm)     {
    std::string const tmp = testdata::inputWithtokens;

    std::string result;
    cm.measure([&](int) {
        result = boost_replace_all(testdata::inputWithtokens, testdata::tokens);
    });
    assert(result == testdata::expected);
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

寻找 C++ 中搜索和替换的圣杯 的相关文章

  • 实体框架 - 循环更新属性

    我正在尝试找到一种方法来循环 EF 对象的属性并更新这些属性的值 更具体地说 我有 50 个字段 其中最多填充 50 个下拉列表 所有 50 个可能都需要填充 也可能不需要填充 为了解决这个问题 我有一个中继器 最多可以创建 50 个 DD
  • 如何将 pem 公钥转换为 openssl RSA* 结构

    假设我必须像这样公开 pem 密钥 BEGIN PUBLIC KEY MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7vbqajDw4o6gJy8UtmIbkcpnk O3Kwc4qsEnSZp TR fQi
  • 最慢的计算复杂度(Big-O)

    在这些算法中 我知道 Alg1 是最快的 因为它是 n 平方的 接下来是 Alg4 因为它是 n 的立方 然后 Alg2 可能是最慢的 因为它是 2 n 这应该具有非常差的性能 然而Alg3和Alg5在我的阅读速度方面还没有遇到过 这两种算
  • VBA 中的 VSTO:AddIn.Object 有时不返回任何内容 (null)

    Given VSTO 插件 An override object RequestComAddInAutomationService 它返回一个名为的类的实例Facade在我的场景中 Excel 2007 中的 VBA 宏可访问AddIn O
  • 如何使用Task.WhenAny并实现重试

    我有一个创建多个基于 I O 的任务的解决方案 我正在使用Task WhenAny 来管理这些任务 但通常许多任务会由于网络问题或请求限制等原因而失败 我似乎找不到一个解决方案 使我能够在使用时成功重试失败的任务Task WhenAny 方
  • 为什么在 Linux 上字符串文字的内存地址与其他字符串文字的内存地址如此不同?

    我注意到字符串文字在内存中的地址与其他常量和变量 Linux 操作系统 非常不同 它们有许多前导零 未打印 Example const char h Hi int i 1 printf p n void h printf p n void
  • 尝试将元素推入向量

    在头文件 我没有编写 中 已经定义了一个结构体 如下所示 struct MemoryMessage public boost counted base public FastAlloc explicit MemoryMessage Memo
  • c#Registry to XML无效字符问题

    我在尝试从注册表创建 XML 文件时遇到问题 在我的笔记本电脑 W7 64b 上它工作正常 生成了 xml 文件 但在另一台计算机 Xp 32b 上抛出异常 System ArgumentException 十六进制值 0x00 是无效字符
  • 是否可以用 C# 为 Android 编写应用程序?

    我们都知道Android运行Dalvik VM程序 通常开发人员用 Java 编写程序并将其编译为 Dalvik 字节码 我想知道是否有可能创建一个可以接受 C 代码并将其编译为 Dalvik 字节码的编译器 嗯 这是一种选择 或者您可以在
  • 使用经度和纬度查找给定距离内的所有附近客户

    我有一个包含客户经度和纬度的数据库 我有一个搜索表单 用户将在其中输入日志 纬度 距离下拉列表包含 50 英里 100 英里 当用户单击搜索时 我想编写一个 linq 查询从数据库中获取此距离半径内的所有客户 如何使用 C 和 linq 来
  • 平衡两轮机器人而不使其向前/向后漂移

    我正在尝试设计一个控制器来平衡 2 轮机器人 约 13 公斤 并使其能够抵抗外力 例如 如果有人踢它 它不应该掉落 也不应该无限期地向前 向后漂移 我对大多数控制技术 LQR 滑模控制 PID 等 都很有经验 但我在网上看到大多数人使用 L
  • 为什么std::string在发布时是标准布局类型,但在调试时不是标准布局类型?

    include
  • 让 WIX 在项目中包含引用

    我对 WiX 和设置自定义安装程序完全陌生 所以我对问题的主题表示歉意 我有一个内部业务应用程序 日记 它构建并运行良好 因此我按照教程 官方文档添加 WiX 项目并引用日记的 csproj 然后构建并运行这个最基本版本的 WiX 安装程序
  • Boost async_write问题

    我将展示一些代码 void wh const boost system error code ec std size t bytes transferred std cout lt lt test int main int argc cha
  • Parallel.For 和 Break() 误解?

    我正在研究 For 循环中的并行性中断 看完之后this http tipsandtricks runicsoft com CSharp ParallelClass html and this http reedcopsey com 201
  • 读取所有进程内存以查找字符串变量c#的地址

    我有 2 个用 C 编写的程序 第一个名为 ScanMe 的程序包含一个包含值 FINDMEEEEEEE 的字符串变量 以及一个值为 1546 22915487 的双精度变量 另一个名为 MemoryScan 的程序读取第一个程序的所有内存
  • 在同一条线上铸造两次

    我在项目中看到了这段代码 b的类型是void void b int a int unsigned long b 这条线毫无意义吗 我的意思是 这与a int b在所有情况下 这可能会避免 64 位 Unix 系统上的编译器警告unsigne
  • 如何从标准输入读取一行,阻塞直到找到换行符?

    我试图从命令行的标准输入一次读取任意长度的一行 我不确定是否能够包含 GNU readline 并且更喜欢使用库函数 我读过的文档表明getline应该可以工作 但在我的实验中它不会阻塞 我的示例程序 include
  • 什么是多重重继承?

    我将以下称为 多重重新继承 直接继承一个类一次 并通过继承其一个或多个后代来间接继承一次或多次 通过继承一个类的两个或多个后代来间接继承一个类两次或多次 我想知道它是否存在以及如何明确访问嵌入的子对象 1 Professional C 2n
  • 在 WPF 树视图中获取 FullPath?

    如果我以编程方式创建 WPF TreeView 例如 TreeView treeView lt added in the designer TreeViewItem rootNode new TreeViewItem rootNode He

随机推荐

  • 位域元素的默认值

    在 C 11 中可以做 struct S int i 42 如果忘记初始化成员i它 默认初始化为 42 我刚刚尝试过 位域为 struct S int i 42 5 我正在得到 错误 预期为 在 标记之前 位域成员是否存在此功能 如果存在
  • JNI 的用处[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 获取文本字段中最常用的 10 个单词

    我有一个包含数千个文档的索引 每个文档都有一个全文字段 我想搜索所有这些字段并获取最常出现的 10 个最常见的单词 如果可能的话 我还想要一种在 Kibana 上可视化它的方法 实现此目的的最常见方法是使用keyword datatype
  • Drupal CCK 的复选框

    我是 Drupal 的新人 到目前为止很喜欢 我正在创建 CCK 自定义内容类型 我需要以复选框格式制作便利设施列表 所以我做了 文件类型 Text 小部件类型 复选框 单选按钮 和允许值列表 onsite dining 现场用餐 Meet
  • 如何在启动时运行命令?

    我试图弄清楚如何在启动时运行命令 就像我将其输入控制台一样 我在 Raspberry Pi 上使用 Rasbian 但我认为这个问题对于 Debian 来说通常是相同的 我尝试运行的命令是 sudo screen mono server e
  • Rails 6 无法连接到 AWS Elastic Beanstalk 预置的 RDS。 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”

    我在尝试向 Elastic Beanstalk 启动示例 Rails 6 应用程序时遇到了非常困难的情况 对于上下文 我遵循这些说明 将 RDS 添加到 Ruby 应用程序 https docs aws amazon com elastic
  • Python 中的线程队列挂起

    我正在尝试通过队列使解析器成为多线程 它似乎有效 但我的队列挂起 如果有人能告诉我如何解决这个问题 我将不胜感激 因为我很少编写多线程代码 此代码从 Q 中读取 from silk import import json import dat
  • 当父视图的任何子视图重绘时,如何触发父视图的重绘?

    背景 我写了一个基于 Android 的自定义视图LinearLayout我称之为ReflectingLayout 这个想法相当简单 在声明的任何子视图下面渲染反射效果ReflectingLayout e g 我通过覆盖来实现这一目标dis
  • C# NHibernate 简单问题

    我在用着NHibernate驱动存储库 Fluent映射并尝试使用Linq to NHibernate 但对于像这样的一些简单查询 Retrieve
  • 解析 HTML 内容时阻止 etree 解析 HTML 实体

    有没有办法阻止etree在解析HTML内容时解析HTML实体 html etree HTML amp html find body text 这给了我 但我想得到 本身 您始终可以对数据进行预处理 后处理 在输入 HTML 解析器之前将 替
  • 如何设置 10 点到 19 点每两小时执行一次 Cron 作业

    1 个月前我对此有一个疑问 那是1个小时的间隔 我得到了确切的答案 以下是旧问题的链接 如何设置从上午 9 00 到下午 6 00 周一至周五 每隔一小时执行一次 Cron 作业 https stackoverflow com questi
  • Python - Tkinter 文本大小未调整大小

    我正在尝试使用 Tkinter 制作一个可以调整大小的窗口 并且效果很好 但我希望字体大小也能按比例缩放 输入框的大小完美调整 但文本大小保持不变 我也可以更改输入文字大小吗 我该怎么做呢 先感谢您 这是到目前为止我的代码片段 defini
  • 压缩 XML 指标。

    我有一个客户端服务器应用程序 它通过 TCP IP 将 XML 从客户端发送到服务器 然后广播到其他客户端 我如何知道 XML 的最小大小可以通过压缩 XML 而不是通过常规流发送来保证性能改进 有什么好的指标或例子吗 Xml 通常压缩得很
  • 为什么有各种 JPEG 扩展名?

    在开发下载器时 我遇到了以下使用 Python 的情况mimetypes guess extension功能 In 2 mimetypes guess extension image jpeg strict False Out 2 jpe
  • 当 wifi 打开时,仅通过 Android 手机上的移动数据发送数据

    即使 wifi 已打开并连接到互联网 是否也可以通过移动数据以编程方式路由请求 我的应用程序需要调用运营商提供的服务 该服务只能通过移动数据使用 而且我认为要求关闭 wifi 不太方便 看一眼https developer android
  • 多对多关系的3表之间的SQL查询

    我有三张桌子 friends locations friend location friend location是一个连接表 允许多对多关系friends and locations 所以表格看起来像这样 Friends ID Name 1
  • WooCommerce:管理员手动创建订单时需要挂钩

    我的网站之一使用 WooCommerce 客户有时希望在订单管理中手动创建订单 WooCommerce gt 订单 gt 添加订单 当他们单击该页面上的 保存订单 时 我需要对订单进行一些额外的处理 有可用的钩子吗 我浏览了 WooComm
  • Microsoft 聊天机器人 (Node.js) 是否在单个 LUIS.AI 应用程序中支持多种语言?

    我有一个使用 Node js 在 Microsoft 机器人框架中构建的聊天机器人 并将该机器人与名为 LUIS AI 智能的 NLP 框架集成 以根据用户的意图和实体处理用户对话 在这里 我需要这个机器人在单个 LUIS 应用程序中支持多
  • awk 查找重叠

    我有一个包含列的文件 如下所示 Group Start End chr1 117132092 118875009 chr1 117027758 119458215 chr1 103756473 104864582 chr1 10509379
  • 寻找 C++ 中搜索和替换的圣杯

    最近 我正在寻找一种替换字符串中标记的方法 这本质上是查找和替换 但至少还有一种解决问题的方法 看起来像是相当平庸的任务 我已经提出了几种可能的实现 但从性能的角度来看 它们都不能令人满意 最好的成绩是每次迭代约 50us 这种情况很理想