简体   繁体   English

cout 语句中的神秘换行符

[英]Mysterious line break in cout statement

I wrote a small snippet to search for matched strings in an array, then output results for parallel arrays in a nicely formatted fashion.我编写了一个小片段来搜索数组中匹配的字符串,然后以格式精美的方式为并行 arrays 生成 output 结果。 However, I must have some fundamental misunderstanding about how string outputs work, because for the life of me I cannot get this to output correctly, no matter where I put the tab, the newline, or whether I use an endl in my code.但是,我必须对字符串输出的工作方式有一些基本的误解,因为在我的一生中,无论我将制表符、换行符放在哪里,或者是否在代码中使用endl ,我都无法正确地将其传递给 output。

Here is the relevant code below:下面是相关代码:

for (int i = 0; i < arrayCount; i++) {
    if (arrayCopy[i].find(localString) != string::npos) {
        cout << "\n\t"
        << array1[i] << " {"
        << subArray1[i] << ", "
        << subArray2[i] << "}";
    }
}

I'm expecting results to have a tab at start of each line:我希望结果在每行的开头都有一个标签:

         MatchedString1 {Data, MoreData}
         MatchedString2 {Data, MoreData}
         MatchedString3 {Data, MoreData}

Instead I am getting results like below, where the tabs appear on blank lines (except for the first result):相反,我得到如下结果,其中选项卡出现在空白行上(第一个结果除外):

         MatchedString1 {Data, MoreData}

MatchedString2 {Data, MoreData}

MatchedString3 {Data, MoreData}

What devilish quirk exists in c++ that is causing me so much pain?! c++ 中存在什么让我如此痛苦的怪癖?!

Using the following source to recreate your problem:使用以下来源重现您的问题:

$ cat test.cpp 
#include <iostream>
#include <string>

int main() {
    using std::cout;
    using std::string;

    const std::string array1[] = {"MatchedString1", "MatchedString2", "MatchedString3"};
    const std::string arrayCopy[] = {"MatchedString1", "MatchedString2", "MatchedString3"}; 
    const std::string localString = "String";
    const std::string subArray1[] = {"Data", "Data", "Data"};
    const std::string subArray2[] = {"More data", "More data", "more data"};
    const unsigned int arrayCount = 3;

    for (int i = 0; i < arrayCount; i++) {
        if (arrayCopy[i].find(localString) != string::npos) {
            cout << "\n\t"
                << array1[i] << " {"
                << subArray1[i] << ", "
                << subArray2[i] << "}";
        }
    }
}

Compile and run:编译并运行:

$ g++ test.cpp 
$ ./a.out

        MatchedString1 {Data, More data}
        MatchedString2 {Data, More data}
        MatchedString3 {Data, more data}

Conclusion :结论

The extra line-breaks are in your data.额外的换行符在您的数据中。

Suggestion :建议

Use square brackets to delineate your input:使用方括号来描述您的输入:

std::cout << "[" << array1[i] << "] {[" << subArray1[i] << "], [" << subArray2[i] << "]}" <<  std::endl;

Stripping the strings :剥离字符串

If you find you need to strip your strings, you may find the following functions useful:如果您发现需要剥离字符串,您可能会发现以下函数很有用:

std::string lstrip(const std::string& s, const char* chars = " \t\r\n")
{
    std::string::size_type begin = s.find_first_not_of(chars);
    if (begin == std::string::npos)
    {
        return "";
    }
    return std::string(s, begin);
}

std::string rstrip(const std::string& s, const char* chars = " \t\r\n")
{
    std::string::size_type end = s.find_last_not_of(chars);
    return std::string(s, 0, end + 1);
}

std::string strip(const std::string& s)
{
    return lstrip(rstrip(s));
}

Use like this:像这样使用:

        std::cout << "\t"
            << strip(array1[i]) << " {"
            << strip(subArray1[i]) << ", "
            << strip(subArray2[i]) << "}"
            << std::endl;

(Restating my comment as a possible answer.) (重申我的评论作为可能的答案。)

The standard approach is to always put the \n at the end and everything else in the order it appears.标准方法是始终将\n放在末尾,其他所有内容都按其出现的顺序放置。

The only thing I can think of that would break this is a \r at the beginning of the second and third matched strings.我能想到的唯一会破坏它的是第二个和第三个匹配字符串开头的\r (Someone else rightly observed that the double-spacing suggests a \n at the beginning of the input.) (其他人正确地观察到双倍行距暗示输入开头的\n 。)

Are you sure this isn't just somethin silly like "MatchedString" actually being "\nMatchedString" ?你确定这不仅仅是像"MatchedString"实际上是"\nMatchedString"的傻事吗? You could try printing some extra stuff there to delineate your whitespace more clearly.你可以尝试在那里打印一些额外的东西来更清楚地描绘你的空白。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM