相关疑难解决方法(0)

在调用std :: numeric_limits <unsigned char>成员之前,一元"+"的目的是什么?

在cppreference的文档中看到了这个例子std::numeric_limits

#include <limits>
#include <iostream>

int main() 
{
    std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

    std::cout << "uchar\t"
              << +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::max() << '\n';
    std::cout << "int\t"
              << std::numeric_limits<int>::lowest() << '\t'
              << std::numeric_limits<int>::min() << '\t'
              << std::numeric_limits<int>::max() << '\n';
    std::cout << "float\t"
              << std::numeric_limits<float>::lowest() << '\t'
              << std::numeric_limits<float>::min() << '\t'
              << std::numeric_limits<float>::max() << '\n';
    std::cout << "double\t"
              << std::numeric_limits<double>::lowest() << '\t'
              << std::numeric_limits<double>::min() << '\t'
              << std::numeric_limits<double>::max() …
Run Code Online (Sandbox Code Playgroud)

c++ char unary-operator

129
推荐指数
4
解决办法
4070
查看次数


cout不打印unsigned char

我正在编写以下代码:

#include<iostream>
#include<stdio.h>

using namespace std;

main() {
    unsigned char a;
    a=1;
    printf("%d", a);
    cout<<a;
}
Run Code Online (Sandbox Code Playgroud)

它打印1和一些垃圾.

为什么cout表现如此?

c++ printf

16
推荐指数
3
解决办法
5万
查看次数

13
推荐指数
3
解决办法
9772
查看次数

uint8_t iostream行为

摘要:我期待代码:cout << uint8_t(0); 打印"0",但它不打印任何东西.

长版本:当我尝试将uint8_t对象流式传输到cout时,我会使用gcc获得奇怪的字符.这是预期的行为吗?可能是因为uint8_t是某些基于字符的类型的别名吗?请参阅代码示例中的编译器/系统说明.

// compile and run with:
// g++ test-uint8.cpp -std=c++11 && ./a.out
//                    -std=c++0x (for older gcc versions)
/**
 * prints out the following with compiler:
 *     gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
 * on the system:
 *     Linux 3.7.9-101.fc17.x86_64
 * Note that the first print statement uses an unset uint8_t
 * and therefore the behaviour is undefined. (Included here for
 * completeness)

> g++ test-uint8.cpp -std=c++11 && ./a.out
>>>?<<<    >>>194<<<
>>><<<    >>>0<<<
>>><<<    >>>0<<< …
Run Code Online (Sandbox Code Playgroud)

c++ iostream c++11

13
推荐指数
2
解决办法
5633
查看次数

使用uint8和uint16的cout << hex的行为

我注意到这cout << hex给了我奇怪的结果,我无法找到解决原因的地方.什么我做的是简单地分配一些值,既uint8_tuint16_t,然后试图将它们写入标准输出.当我运行这个:

uint8_t a = 0xab;
uint16_t b = 0x24de;
cout << hex << a << endl;
cout << hex << b << endl;
Run Code Online (Sandbox Code Playgroud)

我得到了结果:

$./a.out

24de
$
Run Code Online (Sandbox Code Playgroud)

没有显示uint8_t的值.可能是什么导致了这个?我认为不会有一种类型的cout实现而不是另一种类型.

c++ hex stdout

8
推荐指数
3
解决办法
9929
查看次数

奇怪的gcc行为与无符号整数

#include <iostream>

#include <cstdint>
#include <cstdio>

using namespace std;

int main()
{
    uint16_t ii; 
    std::cin >> ii;                                                    
    printf("%d\n", ii);
}
Run Code Online (Sandbox Code Playgroud)

当我输入5输出时也是5.但是当我改变iito 的类型时uint8_t,我没有得到,553似乎是ASCII值5.这是预期的吗?

c++ g++ c++11 c++14

7
推荐指数
1
解决办法
194
查看次数

为什么以十六进制读取uint8_t无法正常工作?

考虑如下代码:

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss;
    ss << "a0 b1";

    uint8_t byte;
    ss >> std::hex >> byte;    

    std::cout << std::hex << byte << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么这个输出a,而不是a0即使a0装配在uint8_t十六进制?

c++ iostream

7
推荐指数
1
解决办法
142
查看次数

std::cout 将 uint8_t 作为字符处理

如果我运行此代码:

std::cout << static_cast<uint8_t>(65);
Run Code Online (Sandbox Code Playgroud)

它会输出:

一种

这是数字 65 的 ASCII 等价物。这是因为它uint8_t被简单地定义为:

typedef unsigned char uint8_t;
Run Code Online (Sandbox Code Playgroud)
  • 这种行为是标准吗?

  • 不应该是定义uint8_t保证被处理为数字而不是字符的更好方法吗?

我无法理解如果我想打印uint8_t变量的值,它将被打印为字符的逻辑。

PS 我使用的是 MSVS 2013。

c++ cout language-lawyer c++11 fixed-size-types

5
推荐指数
1
解决办法
1747
查看次数

将 float 转换为 unsigned char 是有效的转换吗?

有人可以解释一下如何float铸造uint32_t作品吗?这是有效的转换吗?

\n

下面代码中第一行的输出对我来说有意义,但其余的我自己无法弄清楚。

\n
cout<<uint8_t(256+33)<<'\\n';\ncout<<uint8_t(float(256+33))<<'\\n';\ncout<<int(uint8_t(float(256+33)))<<'\\n';\ncout<<int(uint8_t(float(256+3)))<<'\\n';\ncout<<int(uint8_t(float(256+20)))<<'\\n';\n
Run Code Online (Sandbox Code Playgroud)\n

输出

\n
!\n\xef\xbf\xbd\n255\n255\n255\n
Run Code Online (Sandbox Code Playgroud)\n

uint8_t(256+33)是 int 到 unsigned char,它给出!,这正是我所期望的。然而,float根本uint8_t不起作用。当我尝试将输出转换为 int 时,它给了我常量 number 255

\n

我有一个vector<float>并想将其转换为vector<uint8_t>,它将传递到一个函数中。float转换为什么是有效的方法uint8_t?会static_cast<uint8_t>起作用吗?我应该先转换floatint然后再转换intuint8_t吗?

\n

c++ floating-point casting char

5
推荐指数
1
解决办法
246
查看次数

如何在c ++中以大写形式打印地址(十六进制值)

我正在尝试以十六进制格式打印变量的地址(引用).但我发现我能够以大写形式打印等效于77的十六进制,而不是变量的地址(引用).有人可以帮帮我吗?

以下是我遇到困难的程序.

#include <iostream>
#include <string>

using namespace std;


void print_nb_of_items(const int nb_of_apple, const int& nb_of_pens)
{
    cout << "Number of apples = " << nb_of_apple << endl;
    cout << "Number of pens = " << nb_of_pens << " Address = " << uppercase << hex << &nb_of_pens << endl;
    cout << "Hex output in uppercase = " << uppercase << hex << 77 << endl;
}

/* The main function */

int main(int argc, char * argv[])
{
    int …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
4370
查看次数

为什么我不能在uint8_t类型中存储一个小整数?

如果我将数字2存储在uint8_t类型中并尝试打印它我什么也得不到.当然一个8位整数可以存储数字2.我错过了什么?

示例代码:

#include <iostream>
#include <cstdint>

int main() {
    std::uint8_t x = 2;
    std::cout << "x = " << x << "\n";
}
Run Code Online (Sandbox Code Playgroud)

输出:

x = 
Run Code Online (Sandbox Code Playgroud)

c++ types

2
推荐指数
1
解决办法
262
查看次数

为什么通过cin的int8_t和用户输入显示奇怪的结果

一小段代码让我发疯,但希望你可以阻止我跳出窗外.看这里:

#include <iostream>
#include <cstdint>

int main()
{
    int8_t i = 65;
    int8_t j;

    std::cout << "i = " << i << std::endl; // the 'A' is ok, same as uchar

    std::cout << "Now type in a value for j (use 65 again): " << std::endl;
    std::cin >> j;
    std::cout << "j = " << j << std::endl;

    if (i != j) 
        std::cout << "What is going on here?????" << std::endl;
    else 
        std::cout << "Everything ok." << std::endl;

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ int uint8t

-1
推荐指数
1
解决办法
1389
查看次数