Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中通过换行符拆分字符串+;_C++_String_Cout_Iomanip - Fatal编程技术网

C++ 在C+中通过换行符拆分字符串+;

C++ 在C+中通过换行符拆分字符串+;,c++,string,cout,iomanip,C++,String,Cout,Iomanip,如果我有两个存储在std::string变量中的表,如何将它们并排显示?特别是 我有std::string表1,其中包含以下内容: X | Y ------- 2 | 3 1 | 3 5 | 2 X | Y ------- 1 | 6 1 | 1 2 | 1 3 | 5 2 | 3 我有std::string表2,其中包含以下内容: X | Y ------- 2 | 3 1 | 3 5 | 2 X | Y ------- 1 | 6 1 | 1 2 |

如果我有两个存储在
std::string
变量中的表,如何将它们并排显示?特别是

我有
std::string表1
,其中包含以下内容:

 X | Y
-------
 2 | 3
 1 | 3
 5 | 2
 X | Y
-------
 1 | 6
 1 | 1
 2 | 1
 3 | 5
 2 | 3
我有
std::string表2
,其中包含以下内容:

 X | Y
-------
 2 | 3
 1 | 3
 5 | 2
 X | Y
-------
 1 | 6
 1 | 1
 2 | 1
 3 | 5
 2 | 3
我需要修改它们(或者只是将它们打印到标准输出),以便显示以下内容:

 X | Y    X | Y
-------  -------
 2 | 3    1 | 6
 1 | 3    1 | 1
 5 | 2    2 | 1
          3 | 5
          2 | 3
换句话说,我有两个存储在
std::string
变量中的表,用换行符分隔行

我想将它们打印到屏幕上(使用
std::cout
),以便表格并排显示,在顶部垂直对齐。我怎样才能做到这一点?


例如,如果我可以做一些类似于
std::cout关键字:istringstream,getline

实施:

#include <iostream>
#include <sstream>
int main()
{
    std::string table1 = 
        " X | Y\n"
        "-------\n"
        " 2 | 3\n"
        " 1 | 3\n"
        " 5 | 2\n";
    std::string table2 = 
        " X | Y\n"
        "-------\n"
        " 1 | 6\n"
        " 1 | 1\n"
        " 2 | 1\n"
        " 3 | 5\n"
        " 2 | 3\n";

    std::istringstream streamTable1(table1);
    std::istringstream streamTable2(table2);
    while (!streamTable1.eof() || !streamTable2.eof())
    {
        std::string s1;
        getline(streamTable1, s1);
        while (s1.size() < 9)
            s1 += " ";
        std::string s2;
        getline(streamTable2, s2);
        std::cout << s1 << s2 << std::endl;
    }
}
#包括
#包括
int main()
{
std::字符串表1=
“X | Y\n”
“----\n”
“2 | 3\n”
“1 | 3\n”
“5 | 2\n”;
std::字符串表2=
“X | Y\n”
“----\n”
“1 | 6\n”
“1 | 1\n”
“2 | 1\n”
“3 | 5\n”
“2 | 3\n”;
std::istringstream流量表1(表1);
std::istringstream流量表2(表2);
而(!streamTable1.eof()| |!streamTable2.eof())
{
std::字符串s1;
getline(流表1,s1);
而(s1.size()<9)
s1+=”;
std::字符串s2;
getline(流表2,s2);

std::cout-See和。请尝试
std::cout,而不是向s1添加空间