【问题标题】:How to find the index of element (and a few other things)如何找到元素的索引(以及其他一些东西)
【发布时间】:2021-11-04 20:59:52
【问题描述】:

我正在编写一个代码,它将一些随机的 17 个字符串替换为一个字母表,但我找不到方法。基本上,我想做的是:

char strings[] = {
    "L-nIbhm5<z:92~+,x",
    "9bC5f0q@qA(RKZ>|r",
    "9bC5f0q@qA(RKZ>|r",
    "k=5,ln(08IAl(gGAK",
    "|N,8]dGu)'^MaYpu[",
    "!&,Y*nz8C*,J}{+d]",
    "Us9%^%?n5!~e@@*+@",
    "zF8,1KV#¥]$k?|9R#",
    "0B4>=nioEjp>4rhgi",
}

char alphabet[]{
    "a","b","c","d","e","f","g","h","i",
}

replace(std::string str){
    /**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/

int main(){
    cin >> std::string replace;
    replace(replace);

示例输入:L-nIbhm5&lt;z:92~+,x9bC5f0q@qA(RKZ&gt;|r9bC5f0q@qA(RKZ&gt;|r

预期输出:abc

编辑: 新代码 对原代码的改动

它还有一个比简化版本(以前的代码)更大的数组。它显示了整个程序的结构。(字符串被路由到哪里以及为什么)

基本上它在做什么

从用户那里得到输入,放入输入变量中,输入经过算法()函数不变,然后进入替换函数并被替换。然后,被替换的字符串通过原始路由返回到主函数,并在其中显示。

我将数组保留为字符串类型,因为 const char* 给了我一个分段错误。

std::string Subs[53]=
{
    "LQlMv]G5^^1kcm?fk",
    "7W^S;/vB(6%I|w[fl",
    "<w7>4f//Z55ZxK'z.",
    "_W5g(lu<pTu3^_A7n",
    "OfLm%8:EF}0V1?BSS",
    "|+E6t,AZ~XewXP17T",
    "L-nIbhm5<z:92~+,x",
    "L-nIbhm5<z:92~+,x",
    "9bC5f0q@qA(RKZ>|r",
    "9bC5f0q@qA(RKZ>|r",
    "k=5,ln(08IAl(gGAK",
    "|N,8]dGu)'^MaYpu[",
    "!&,Y*nz8C*,J}{+d]",
    "Us9%^%?n5!~e@@*+@",
    "zF8,1KV#¥]$k?|9R#",
    "0B4>=nioEjp>4rhgi",
    "EG@0[W9.N4i~E<f3x",
    "(0Pwkk&IPchJHs.7A",
    "7XgmQ6fW<|J+NY[m0",
    ".g4CwX/DU!!~!zbtZ",
    "+_U'qn_/9Fo|gT/!n",
    "=0s(mYh&F%y=MBS5(",
    "cg71(}bo+Q5P8F[T6",
    "lc|a\%5.9pOpooU+QR",
    "E_(3A:o+.]qL3MYA6",
    "H@O'X_RiVS@8l0bKD",
    "Y1gbGD`~8d>HSWN35",
    "LQlMv]G5^^1kcm?fk",
    "T4}gI;`BFVfhw=-sf",
    "6BHMA0IRix]/=(jht",
    "yS$=@Jdpp?P2k6SMQ",
    "t1~|kkh+>4d>}OQ`a",
    "2Y-\\CU\"944yBluWD5",
    "'M\\ZbIX5{`Xd;qi!o",
    "?N+RtVqj_r(C5@#0\"",
    "2;*Livh?V$X/8z@Md",
    ")IN|7FOs2l-mAM[d@",
    "(~f268J},xXrK'Rp'",
    "&r/qf9fFHnzV!RzH/",
    "}naDRH4p$NI2a).t,",
    "{8DM+7!.Mge|~fnO|",
    ")r[@nI0YDH>6cE38p",
    "(0Pwkk&IPchJHs.7A",
    ")r[@nI0YDH>6cE38p",
    "8M-=cQFQ,pPo7eu=p",
    "0PHw=/|(tZ1}FHm/'",
    "[su`'0Oybc.\"-/W5)",
    "1uHl[IC7Sr#NUJV;I",
    "8z8%,jK0CDOkJz8I?",
    "3Ao2yXDN%YzpE&Suy",
    "zNs`7E'e/$i8VqaUL",
    "bzHmA^K2>7`UZ?!AO",
};

std::string Alphabet[53] = 
{
    " ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","r","w","x","y","z",
    "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
};


std::string replace(std::string rep) {
    int len = sizeof(Subs)/sizeof(Subs[0]);
    std::stringstream ss1;
    for(int i = 0; i < len; i++) {
        if (rep.find(Subs[i]) != std::string::npos) {
            ss1 << Subs[i];
        }
    }
    std::string input = ss1.str();
    return input;
}

std::string algorithm(std::string input)
{
    //some other algorithms come here(not relative to this question)
    input = replace(input);
    return input;
}

int main(void){
    int ed;
    std::cin >> ed;

    if(ed == 1){
//different function(not relative to the question)
        }
    else if(ed == 0){
        std::string input;
        std::cin >> input;
        input = algorithm(input);
        std::cout << input << std::endl;
    }

    else{
        std::cout << "1 or 0" << std::endl;
        main();
    }

return 0;
}

示例输入:L-nIbhm5&lt;z:92~+,x9bC5f0q@qA(RKZ&gt;|r9bC5f0q@qA(RKZ&gt;|r

预期输出:abc

实际输出:L-nIbhm5&lt;z:92~+,xL-nIbhm5&lt;z:92~+,x9bC5f0q@qA(RKZ&gt;|r9bC5f0q@qA(RKZ&gt;|r

对不起,它变长了。

【问题讨论】:

    标签: c++ arrays string c++17 substitution


    【解决方案1】:

    上面的代码有几个错误:

    • char array 初始化不正确。
    • method body 用于 mainreplace 方法不是 closed
    • 目前replace方法的默认返回类型为int

    这里有string#find 方法可以提供帮助。

    我已尝试进行这些修复,这里是 C++17 中的更新代码:

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    const char *strings[9] = {
        "L-nIbhm5<z:92~+,x",
        "9bC5f0q@qA(RKZ>|r",
        "9bC5f0q@qA(RKZ>|r",
        "k=5,ln(08IAl(gGAK",
        "|N,8]dGu)'^MaYpu[",
        "!&,Y*nz8C*,J}{+d]",
        "Us9%^%?n5!~e@@*+@",
        "zF8,1KV#¥]$k?|9R#",
        "0B4>=nioEjp>4rhgi"
    };
    
    const char *alphabet[9] = {
        "a","b","c","d","e","f","g","h","i"
    };
    
    void replace(std::string rep) {
        int len = sizeof(strings)/sizeof(strings[0]);
        std::stringstream ss1;
        for(int i = 0; i < len; i++) {
            if (rep.find(strings[i]) != std::string::npos) {
                ss1 << alphabet[i];
            }
        }
        std::cout << ss1.str();
    }
    
    int main(){
        std::string rep;
        cin >> rep;
        replace(rep);
    }
    

    供参考:https://onlinegdb.com/Bd9DXSPAa

    注意-以上代码仅供参考,请务必添加所有测试用例处理。

    【讨论】:

    • 好吧只是为了快速尝试我补充说,现在我们可以用#include &lt;sstream&gt;替换它。更新了代码。
    • 代码完全按照您编写的方式独立运行,但是当我尝试将更多元素添加到数组中时(就像整个字母表一样,它不起作用(不是错误,它只是作为 fghiZ 出现)
    • 您能否在示例输入和输出中添加该示例?
    • 我已经编辑了这个问题,以便它勾勒出整个程序的完整图像。我还添加了实际输入和输出以及预期输出。它与我之前的评论有不同的输出,这是因为我之前的评论是独立的,而编辑后的答案是一个整体。我也想将独立程序放入编辑中,但认为编辑会变得太长。
    【解决方案2】:

    我为您的代码制作了一个 c++17 版本。 用 C++ 风格的容器、迭代器替换 'c' 风格的数组和指针。 并使用 std::string::replace 函数。如果可以,请使用标准库, 它经过测试并有据可查。

    #include <algorithm>
    #include <iostream>
    #include <regex>
    #include <string>
    #include <vector>
    
    // std::vector/std::array instead of 'c' style arrays.
    // allows us to us range based for loops later.
    std::vector<std::string> strings =
    {
            "L-nIbhm5<z:92~+,x",
            "9bC5f0q@qA(RKZ>|r",
            "k=5,ln(08IAl(gGAK",
            "|N,8]dGu)'^MaYpu[",
            "!&,Y*nz8C*,J}{+d]",
            "Us9%^%?n5!~e@@*+@",
            //"zF8,1KV#¥]$k?|9R#", // <<== I commented out this line, ¥ is not a valid charcter in my environment
            "0B4>=nioEjp>4rhgi"
    };
    
    // a string is already an array of characters.
    std::string alphabet{ "abcdefghijkl" };
    
    std::string replace_with_alphabet(const std::string& input)
    {
        std::string retval{ input };
        std::size_t index{ 0 };
    
        // range based for, it will keep the order of the vector.
        for (const auto& str : strings)
        {
            // look if you can find any of the predefined strings 
            // in the input strings.
            const size_t pos = retval.find(str, 0);
    
            // if found
            if (pos != std::string::npos) 
            {
                // get the next character from the alphabet
                std::string replacement{ alphabet[index++] };
    
                // use std::string::replace for replacing the substring
                const size_t len = str.length();
                retval.replace(pos, len, replacement, 0);
            }
        }
    
        return retval;
    };
    
    /**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/
    int main()
    {
        auto output = replace_with_alphabet("L-nIbhm5<z:92~+,x9bC5f0q@qA(RKZ>|rk=5,ln(08IAl(gGAK");
        std::cout << output << std::endl;
    }
    

    【讨论】:

    • 您也可以在 C 数组上使用范围。
    • 通过就地修改retval,如果插入的元素与另一个替换匹配,您可能会遇到问题
    猜你喜欢
    • 2021-12-30
    • 2011-11-18
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多