我有一些需要经常打印的结构.现在,我正在使用这个结构的经典打印包装器:
void printf_mystruct(struct* my_struct)
{
if (my_struct==NULL) return;
printf("[value1:%d value2:%d]", struct->value1, struct->value2);
}
Run Code Online (Sandbox Code Playgroud)
这个功能很方便,但也非常有限.我不能在不制作新包装的情况下预先添加或附加一些文本.我知道我可以使用va_arg系列来预装或推出一些文本,但我觉得我会重新实现这个轮子.
我想知道是否可以为printf编写自定义功能.我希望能够写出这样的东西:
register2printf("%mys", &printf_mystruct);
...
if (incorrect)
printf("[%l] Struct is incorrect : %mys\n", log_level, my_struct);
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?我怎样才能做到这一点 ?
注意:我在Ubuntu Linux 10.04下使用gcc.
我正在使用MinGW的g ++测试我在网上发现的一些片段.这是C++编译器...为什么它才能正确编译C ....为什么人们会交织C和C++.
具体问题是:使用C和C++并在g ++下编译是否可以.如果答案是肯定的,那么我的生活很简单,因为我不需要修改代码.
奇怪的是......让一些C++工作,特别是在将字符串传递给ifstream构造函数时,它需要一个C类型的字符串......
我的猜测是,因为C++有时会依赖于C构造,所以可以将两种语言一起编写.
但是,作为一种风格问题,你应该坚持cout/ cin或printf/ scanf.
我知道 c++20 格式提案是libfmt部分的形式化,而 libfmt 是该形式化的兼容实现。但是,据我所知,libfmt 提供的功能超出了 c++20 标准中指定的功能。有哪些附加功能?
另外,主要编译器供应商是简单地包含 libfmt 的一个子集还是重新实现它?
我在很多地方看过这个,但不明白.为什么说cout比printf()更安全.仅仅因为它不需要写%d %c %f或它有更深层的含义.
提前致谢.
谁能告诉我std::cerr和之间的用法区别是什么perror
void perror ( const char * str );
Run Code Online (Sandbox Code Playgroud)
我想知道在 C++ 应用程序中哪一个更可取,为什么它更可取。
对于这个数组,尝试这样的事情:
void rollover(int val,int count) {
if(count==0) {
return;
}
printf("%d ",val);
count--;
rollover(val,count);
}
int main() {
int arr[]={0,1};
for(int i=0;i<=1;i++) {
rollover(arr[i],4);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用递归方法的预期输出:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Run Code Online (Sandbox Code Playgroud)
无法理解如何编写rec函数.我花了几个小时来解决它.有人可以协助编写该功能吗?
我正在尝试做下面发布的G_G之类的事情.我怎么能写这样的递归函数?我是否必须使用一个for循环来调用递归函数,或者使用两个for循环来递归,还是应该调用两次递归函数?例如:
void rollover(int val,int count) {
if(count==0) {
return;
}
printf("%d ",val);
count--;
rollover(val,count);
//.. do something if necessary ..
rollover(val,count);
//.. do something if necessary ..
}
Run Code Online (Sandbox Code Playgroud) 最近,我对指针感到好奇,而且我不明白为什么以下命令不起作用。
我的想法是创建一个变量,说出它的地址(十进制),要求用户将其输入回来,并将其作为指向前一个变量的指针的地址。最后,两个变量应具有相同的值。
最后一个指针应该指向一个错误的内存地址,因为它存在段错误,但是当我打印指针的地址时,它们是相同的!
#include <iostream>
int main() {
int temp = 0;
printf("Temporary integer addr 0x%x (dec %d)\n", &temp, &temp);
int input = -1;
printf("Give me an address to read (in dec)\n");
std::cin >> input;
printf("You chose the number %d (hex 0x%x)\n", input, input);
int* intPtr = (int*)input;
int* tempPtr = &temp;
if (intPtr == tempPtr) {
printf("It works, they are equal!\n");
} else {
printf("Expected: 0x%x got 0x%x\n", tempPtr, intPtr);
printf("Expected value: 0x%d got 0x%d", temp, *intPtr); // segfaults …Run Code Online (Sandbox Code Playgroud) 我需要为我的教育编写一个乐透生成器,它会随机滚动数字并检查重复条目,否则替换它们。当我启动程序时没有错误消息并且程序运行但我只看到奇怪的字符而不是数字。 问题的图片
我的代码有什么问题?
#include <iostream>
#include <array>
#include <time.h>
std::array<unsigned char, 6> lottoZahlen = {0, 0, 0, 0, 0, 0};
void arrayFuellen();
unsigned char checkDuplikate(unsigned char);
void arraySortieren();
int main()
{
arrayFuellen();
arraySortieren();
std::cout << "\n---- Ihre Glueckszahlen lauten: ----" << std::endl;
for (unsigned char lottoGlueck : lottoZahlen)
{
std::cout << lottoGlueck << std::endl;
}
std::cout << "---- Glueckszahlen Ende ----" << std::endl;
}
void arrayFuellen()
{
srand(time(NULL));
unsigned char wuerfelZahl = 0;
unsigned char wuerfelZahlChecked = 0;
for …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main ()
{
// code
}
return 0 ;
Run Code Online (Sandbox Code Playgroud)
#include<iostream>
int main ()
{
// code
}
Run Code Online (Sandbox Code Playgroud)
哪个库最好用?
什么是最好的,为什么?当我编码它们之间的功能差异时?