T& f() { // some code ... }
const T& f() const { // some code ... }
Run Code Online (Sandbox Code Playgroud)
我现在已经看过几次了(在我迄今为止研究的介绍性书中).我知道第一个const使返回值为const,换句话说:不可修改.我相信第二个const允许为const声明的变量调用该函数.
但是为什么你会在同一个类定义中同时拥有这两个函数?编译器如何区分这些?我相信第二个f()(带const)也可以调用非const变量.
我正在阅读CS:APP,并且关于强制转换,它说当从int转换为float时,数字不能溢出,但它可能是舍入的.
这对我来说似乎很奇怪,因为我不知道有什么回合,所以我已经尝试过了.我认为这只会是非常大的整数(在INT_MAX/ 附近INT_MIN)的情况,但是四舍五入也会发生在四亿左右.(不确定这首先发生在哪里).
为什么会这样?float远远超过的范围int.有人可能会说浮点数不能准确表示,但是从转换int到时double,值没有变化.double超越的优点float是它具有更大的范围和精度.但是float仍然有足够的范围来"封装"整数,并且精度应该不重要,因为整数没有小数位(好吧,全部为0),或者我认为错了?
这是我得到的一些输出(这里是代码:http://pastebin.com/K3E3A6Ni):
FLT_MAX = 340282346638528859811704183484516925440.000000
INT_MAX = 2147483647
(float)INT_MAX = 2147483648.000000
(double)INT_MAX = 2147483647.000000
INT_MIN = -2147483648
(float)INT_MIN = -2147483648.000000
====other values close to INT_MIN INT_MAX====
INT_MAX-1 = 2147483646
(float)INT_MAX-1 = 2147483648.000000
INT_MIN+1 = -2147483647
(float)INT_MIN+1 = -2147483648.000000
INT_MAX-2 = 2147483645
(float)INT_MAX-2 = 2147483648.000000
INT_MAX-10 = 2147483637
(float)INT_MAX-10 = 2147483648.000000
INT_MAX-100 = 2147483547
(float)INT_MAX-100 …Run Code Online (Sandbox Code Playgroud) 有没有办法在 C 中访问十六进制数的某些部分?
我想编写一个函数,它接受一个十六进制数并将其取反,但保持最低有效字节不变。例如:0x87654321 应该变成 0x789ABC21。
我试图以某种方式保存 LSB,然后将其应用于否定的 x,但我的问题是我的掩码被应用于所有字节。我可以针对特定值对其进行硬编码,但显然这不是我想要的。
void b(int x) {
int temp = x & 0xFF; // temp is all 0 with the LSB being the same as x's
x = ~x; // this negates x
// one of a couple of attempts I tried thus far:
// x = (x & 0x00) | temp;
// idea: change x's LSB to 00 and OR it with temp,
// changing x's LSB to temp's LSB
}
Run Code Online (Sandbox Code Playgroud)
如果您不发布解决方案的代码,我会很感激,而只是回答是否有一种方法可以将位操作应用于十六进制数的特定部分,或者我可能如何解决这个问题。
我有,我有一个一流的Maven项目SomeClass中src/main/java是应该读出位于文件src/main/resources.
这是我的第一个方法:
private static void someMethod() {
// ... [context] ...
String fileName = "src/main/resources/some_file.txt";
for(String line : Files.readAllLines(Paths.get(fileName))) {
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud)
这在Eclipse控制台上运行时工作正常,但是当我在Windows控制台上执行jar文件(由Maven生成)时(它IOException被抛出)它不会.
我认为它与文件路径有关,所以我将代码更改为:
private static void someMethod() {
// ... [context] ...
String fileName = "C:/Users/.../workspace/.../src/main/resources/some_file.txt";
for(String line : Files.readAllLines(Paths.get(fileName))) {
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于Eclipse和Windows控制台.但显然这不适用于另一台机器.
在搜索时我将静态方法更改为非静态方法,并用于ClassLaoder获取文件:
private void someMethod() {
// ... [context] ...
ClassLoader cL = getClass().getClassLoader();
File file = new File(cL.getResource("some_file.txt").getFile());
for(String line : Files.readAllLines(Paths.get(file.getPath()))) { …Run Code Online (Sandbox Code Playgroud) #include <string>
#include <iostream>
using namespace std;
const string& strReverse(const string&);
int main() {
cout << strReverse("POT") << endl;
}
const string& strReverse(const string& s) {
static string ret;
ret = "";
for(string::size_type i = s.length()-1; i >= 0; --i) {
ret += s[i];
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码会导致程序在运行时崩溃.但是,如果我改变的类型i,以int它的工作原理.
这是为什么?我认为使用string::size_type比使用特定类型更"安全" int.使用auto也会导致程序崩溃.
我认为类型可能string::size_type与数组索引不兼容,所以我尝试将索引转换i为int,但这也不起作用.