简体   繁体   English

将printf更改为cout

[英]changing printf to cout

I am still a bit unfamiliar with C++ and need some help with using cout . 我仍然不太熟悉C ++,在使用cout需要一些帮助。

int main()
{
    char letterGrades[25] = { 'a', 'b', 'c', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', };

    for (int i = 0; i < 25; i++)
    {
        printf("[%d] --> %c", i, letterGrades[i]);

        if (i == 3)      // how can I print \n when i == 7 , 11 , 15 , 19....
        {
            printf("\n");
        }
    }
}

This is what I am trying to do, and it works perfectly fine. 这是我正在尝试做的,并且工作得很好。 However, I don't know how to write this code using cout. 但是,我不知道如何使用cout编写此代码。

Also, I would print the result in a 4 in a row tabulate format. 另外,我将结果以4列的表格格式打印。 so result can look something like this 所以结果可以像这样

[0] --> A [1] --> A [2] --> A [3] --> A
[4] --> A [5] --> A [6] --> A [7] --> A
[8] --> A [9] --> A [10] --> A [11] --> A

The class of which cout is an instance has clever overloads to << for many types, including char , int , and const char[] (for string literals): cout是实例的类对于许多类型都有聪明的重载, << ,包括charintconst char[] (对于字符串文字):

So you can write 所以你可以写

std::cout << "[" << i << "] --> " << letterGrades[i];

in place of your first printf and 代替您的第一个printf

std::cout << "\n";

for the second one. 第二个。 You can use "\\t" to inject a tabulation character into the stream. 您可以使用"\\t"将制表符插入流中。

All this comes at a slight performance hit, which ought to be negligible cf. 所有这些都对性能造成了轻微的影响,这应该可以忽略不计。 the I/O on your platform. 您平台上的I / O。

Also, consider reading C++: "std::endl" vs "\\n" for further study. 另外,请考虑阅读C ++:“ std :: endl”与“ \\ n”进行进一步研究。

Finally, use 最后,使用

if (i % 4 == 3){
    // i is 3, 7, 11, 15, 19, etc
}

for your conditional check for the periodic newlines. 用于定期换行的条件检查。 % is the remainder operator. %余数运算符。

Inside for loop something like this: 在for循环中,如下所示:

std::cout << "[" << i << "]" << "-->" << letterGrades[i]; 
if (i == 3){ 
     std::cout << "\n"; 
}

its pretty easy actually 其实很容易

for (int i = 0; i < 25; i++)
{
 cout << "[" << i << "] --> " << letterGrades[i] << (i%3==0)?endl:""; 
}

Let me explain the code 让我解释一下代码

  1. cout takes bitwise operations to pass in a stream. cout需要按位操作才能在流中传递。 What that basically means is that you need to use "<<" operator to tell it what to print. 这基本上意味着您需要使用“ <<”运算符来告诉它要打印什么。

  2. The last bit (i%3==0)?endl:"" . 最后一位(i%3==0)?endl:"" If you're not familiar with the ? 如果您不熟悉? operator, what that does is if the condition that appears within () is true, it'll evaluate the part before : , if not then it'll evaluate the later 运算符,它的作用是,如果()中出现的条件为true,它将评估:之前的部分,否则,将评估后面的部分。

eg:- 例如:-

print((1>0)?"Hello":"World") \\ Output: Hello
print((false)?"Hello":"World") \\ Output: World
  1. % is also a neat operator we can use. %也是我们可以使用的整洁运算符。 It divides a number and returns the remainder. 它除以数字并返回余数。

eg:- 例如:-

print(10%5) \\ Output: 0
print(5%2) \\ Output: 1

So I used that to see if there should be a line terminator endl ("\\n") after the third column, 所以我用它来看看在第三列之后是否应该有一个行终止符endl (“ \\ n”),

Hope this helps 希望这可以帮助

The easiest way to insert a newline (or std::endl) after each 3rd, 7th, 11th, etc, line is probably to have an int to count each step 在第3、7、11等行之后插入换行符(或std :: endl)的最简单方法可能是让int计数每一步

int endl_count = 1;
for (int i = 0; i < 25; i++)
  {
    std::cout << '[' << i << ']' << " --> " << letterGrades[i];
    if (endl_count == 3) {
      endl_count = 0;
      std::cout << std::endl;
    }
    endl_count++;
  }

Also: to get the size of an array you can do 另外:要获取数组的大小,您可以执行

(sizeof(array) / sizeof(datatype))

So in your case you can do 所以你可以

for (int i = 0; i < (sizeof(letterGrades) / sizeof(char)); i++)

instead of hardcoding the 25 而不是硬编码25

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

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