如何打印一组要输出的字符串?C++
How to print a set number of strings to output? C++
我很难从两个单独的链表中打印数据。我有合适的宽度和缩进。我只是不知道如何让数据每行只打印8个字符串。
我的打印功能
void print(string print_data, int node_no) {
Node* p = head;
Node* temp;
int i;
int number = node_no;
if(node_no == 0 || node_no == 1) {
temp = p;
}
else {
i = 1;
while(i < node_no) {
i = i +1;
temp = p->next;
p = p->next;
}
}
cout << left;
for(int i = 0; i < temp->data; ++i) {
cout << setw(4) << print_data << " ";
}
}
此外,我不能通过使用模b/c来设置这一点。我一次从每个列表中打印一个节点,所以我在for循环中的I不能用来确定何时结束一行。
如果我正确理解您的问题,那么在执行print()
函数底部的循环时,您不知道给定行上已经打印了多少字符串。如果这就是问题所在,那么您需要将关于有多少字符串已经在某个地方的行中的信息存储起来。存储该数据的一个位置可以是流的iword()
:
// at an appropriate location, probably at namespace scope outside the function:
static int const iword_index = std::ios_base::xalloc();
// ...
for(int i = 0; i < temp->data; ++i) {
out << setw(4) << print_data << " ";
if (++out.iword(iword_index) == 8) {
out << 'n';
out.iword(iword_index) = 0;
}
}
原则上,也可以创建一个适当的过滤流缓冲区,它跟踪每行的字数。虽然这是一个更通用的解决方案,但也有点复杂。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词