Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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_Printing - Fatal编程技术网

C++ 在c+中的行上的特定位置打印输出+;

C++ 在c+中的行上的特定位置打印输出+;,c++,string,printing,C++,String,Printing,在打印出标题后,我试图打印到一行中的统一位置。下面是一个例子: PHRASE TYPE "hello there" => greeting "yo" => greeting "where are you?" => question "It's 3:00" => statement "Wow!" => exclamation 假设每一个都存储在s

在打印出标题后,我试图打印到一行中的统一位置。下面是一个例子:

PHRASE                 TYPE
"hello there"       => greeting
"yo"                => greeting
"where are you?"    => question
"It's 3:00"         => statement
"Wow!"              => exclamation
假设每一个都存储在
std::map
中,其中key=phrase,value=type。我的问题是,仅仅使用选项卡依赖于我查看输出的控制台或文本编辑器。如果标签宽度太小,我就不知道在哪里打印。我尝试过使用
setw
,但这只会将分隔符(=>)打印到短语末尾的固定距离。有没有一个简单的方法可以做到这一点


注意现在假设我们总是知道短语长度不会超过16个字符。如果是这样,我们不需要考虑该怎么做。

如果您无法使用setw,一个简单的替代方法是尝试用空格填充所有短语,使它们都有16个字符长。

使用和:


std::cout如果您不反对C风格的打印,
printf
非常适合这种类型的打印,而且可读性更高:

printf("\"%16s\" => %s\n", it->first.c_str(), it->second.c_str());
在C++程序中使用Prtf和朋友没有什么错,只是小心地混合IoSoots和STDIO。您总是可以将sprintf放入缓冲区,然后用iostreams输出该缓冲区

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

与Peter的解决方案相同,但在引号外添加了填充。它使用带有长度参数的
%c
插入填充。

我个人认为c样式打印对于格式化打印更具可读性。使用
printf
还可以使用
*
格式化程序处理列宽

#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  
}

您可能会发现此函数很有用:

#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);
}
#包括
#包括
空白printRightPadded(标准::ostream&stream,常数标准::string&s,大小和宽度)
{
std::ios::fmtflags old_flags=stream.setf(std::ios::left);

stream
setw
确实是你在这里的朋友。你能发布你的代码吗?也许你把它放在了一个错误的地方。这将把结束引号放在填充空格之后。你省略了引号,但这个解决方案不考虑它们。如果你把引号添加到这个解决方案中,结束引号将放在填充空格之后。你是u唱
setw(16)
,但它应该是
setw(18)
,因为引号现在算在你的长度上了。@StilesCrisis,好的。我会把它留给OP。对于填充,你可以做
%*c
并给出
16-it->first.size(),“
%*s
.”
。这样你就不需要在填充物上加硬帽子,也不需要额外的字符串。好主意,我已经切换到了
%*c
方法。也许
%-*s=>%s
会更好地复制用户想要的内容。@Shahbaz我提到过,但你是对的,我可能应该让它更清晰可见。
printf("\"%s\"%*c => %s", 
    it->first.c_str(), 
    std::max(0, 16 - it->first.size()),
    ' ',
    it->second.c_str());`
#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  
}
Hello     World     
Hello          World          
#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);
}
void
  printKeyAndValue(
    std::ostream &stream,
    const std::string &key,
    const std::string &value
  )
{
  printRightPadded(stream,"\"" + key + "\"",18);
  stream << " => " << value << "\n";
}