简体   繁体   English

似乎无法打印出我的字符串

[英]Can't seem to print my string out

So this is a fragment of my code:所以这是我的代码片段:

    void reverse(string query, string reverseQuery) {
        unsigned int i;

        for(i=0; i<query.length(); i++) {
            reverseQuery[i] = query[query.length()-1-i];
        }
        cout << reverseQuery << endl;
        return;
    }

The headers for iostream, string and using namespace std were also included in the code. iostream、string 和 using namespace std 的标头也包含在代码中。 The problem I am facing is that when I try to output the string reverseQuery nothing comes out.我面临的问题是,当我尝试输出字符串 reverseQuery 时,什么也没有出现。 Anyone knows why?有谁知道为什么? Thanks!谢谢!

Your fragment should look like:您的片段应如下所示:

std::string reverse(const string& query) {
    std::string reverseQuery(query.length(),0); // <<<<< Ensure that the size is the same
    for(unsigned i=0; i<query.length(); i++) {
        reverseQuery[i] = query[query.length()-1-i];
    }
    // cout << reverseQuery << endl;
    return reverseQuery;
}

The way shorter and idiomatic code to achieve that with a standard c++ string is使用标准 C++ 字符串实现该目标的更短且惯用的代码的方式是

std::string reverseQuery(query);
std::reverse(std::begin(reverseQuery),std::end(reverseQuery));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能打印出我的字符串数组c ++? - Why can't I print out my string array c++? 我似乎无法弄清楚为什么我的合并排序这么慢 - I can't seem to figure out why my merge sort is so slow 我似乎无法弄清楚为什么我对文件的读/写功能不起作用 - I can't seem to figure out why my read/write to a file functions are not working 似乎无法调用我的函数 - Can't seem to call my function Domino程序。 我不知道如何将向量拉成空白以将其打印出来 - Dominoes program. I can't figure out how to pull my vector into a void to print it out 似乎无法使用对代理类中的 for 循环的调用从主打印字符串数组(agents.cpp) - Can't seem to print string array(agents.cpp) from the main using a call to a for loop within the agents class 为什么我不能从抛出异常中打印出错误? - How come I don't can't print out error from my throw exception? 操作数组超出范围,似乎无法理解含义 - Manipulating array out of bounds, can't seem to understand implications 有没有办法解决分段错误? 我似乎无法弄清楚 - Is there a way to fix the segmentation fault ? I can't seem to figure it out 为什么不能打印出宏创建的结构的所有成员名称? - Why can't I print out all of the member names of my macro-created struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM