简体   繁体   English

打印到c ++中在线的特定位置输出

[英]print to output at specific location on line in c++

I am trying to print to a uniform position in a line after printing out a header. 我打算在打印出标题后尝试打印到一行中的统一位置。 here's an example: 这是一个例子:

PHRASE                 TYPE
"hello there"       => greeting
"yo"                => greeting
"where are you?"    => question
"It's 3:00"         => statement
"Wow!"              => exclamation

Assume each of these are stored in a std::map<string, string> , where key = phrase and value = type. 假设每个都存储在std::map<string, string> ,其中key = phrase和value = type。 My issue is that simply using tabs is dependent on the console or text editor that I view the output in. If the tab width is too small I won't know for sure where it will be printed. 我的问题是简单地使用制表符取决于我查看输出的控制台或文本编辑器。如果制表符宽度太小,我将无法确定它将在何处打印。 I have tried using setw , but that only prints the separator ("=>") a fixed distance from the end of the phrase. 我尝试过使用setw ,但只打印分隔符(“=>”)与短语末尾相距固定的距离。 Is there a simple way to do this? 有一个简单的方法吗?

NOTE Assume for now that we just always know that the phrase will not be longer than, say, 16 characters. 注意现在假设我们总是知道短语不会超过16个字符。 We don't need to account for what to do if it is. 如果是的话,我们不需要考虑该怎么做。

Use std::left and std::setw : 使用std::leftstd::setw

std::cout << std::left; // This is "sticky", but setw is not.
std::cout << std::setw(16) << phrase << " => " << type << "\n";

For example: 例如:

#include <iostream>
#include <string>
#include <iomanip>
#include <map>

int main()
{
    std::map<std::string, std::string> m;
    m["hello there"]    = "greeting";
    m["where are you?"] = "question";

    std::cout << std::left;

    for (std::map<std::string, std::string>::iterator i = m.begin();
         i != m.end();
         i++)
    {
        std::cout << std::setw(16)
                  << std::string("\"" + i->first + "\"")
                  << " => "
                  << i->second
                  << "\n";
    }
    return 0;
}

Output: 输出:

"hello there"    => greeting
"where are you?" => question

See http://ideone.com/JTv6na for demo. 有关演示,请参见http://ideone.com/JTv6na

printf("\"%s\"%*c => %s", 
    it->first.c_str(), 
    std::max(0, 16 - it->first.size()),
    ' ',
    it->second.c_str());`

The same idea as Peter's solution, but puts the padding outside the quotes. 与Peter的解决方案相同的想法,但将填充放在引号之外 It uses %c with a length argument to insert padding. 它使用带有length参数的%c来插入填充。

If you're not adverse to C-style printing, printf is great for this sort of thing, and much more readable: 如果你不喜欢C风格的打印, printf非常适合这种事情,并且更具可读性:

printf("\"%16s\" => %s\n", it->first.c_str(), it->second.c_str());

There's nothing wrong with using printf and friends in a C++ program, just be careful mixing iostreams and stdio. 在C ++程序中使用printf和朋友没什么不对,只要小心混合iostreams和stdio。 You can always sprintf into a buffer, then output that with iostreams. 你总是可以sprintf到缓冲区,然后用iostreams输出。

You might find this function useful: 您可能会发现此功能很有用:

#include <iostream>
#include <iomanip>

void printRightPadded(std::ostream &stream,const std::string &s,size_t width)
{
  std::ios::fmtflags old_flags = stream.setf(std::ios::left);
  stream << std::setw(width) << s;
  stream.flags(old_flags);
}

You could use it like this: 你可以像这样使用它:

void
  printKeyAndValue(
    std::ostream &stream,
    const std::string &key,
    const std::string &value
  )
{
  printRightPadded(stream,"\"" + key + "\"",18);
  stream << " => " << value << "\n";
}

如果你无法使用setw,一个简单的替代方法是用空格修补所有短语,这样它们都是16个字符长。

I personally find the C-style printing more readable for formatted printing. 我个人觉得C风格的打印对于格式化打印更具可读性。 Using printf you could also handle the column width using the * formatter. 使用printf您还可以使用* formatter处理列宽。

#include <cstdio>

int main() {
    printf("%-*s%-*s\n", 10, "Hello", 10, "World");
    printf("%-*s%-*s\n", 15, "Hello", 15, "World");  

    // in the above, '-' left aligns the field
    // '*' denotes that the field width is a parameter specified later
    // ensure that the width is specified before what it is used to print  
}

Output 产量

Hello     World     
Hello          World          

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

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