小编akr*_*ki1的帖子

C++中赋值语句的评估顺序

map<int, int> mp;
printf("%d ", mp.size());
mp[10]=mp.size();
printf("%d\n", mp[10]);
Run Code Online (Sandbox Code Playgroud)

此代码产生的答案不是很直观:

0 1

我理解为什么会发生这种情况 - 赋值的左侧返回对mp[10]底层值的引用,同时创建上述值,然后使用新计算size()的映射来评估右侧.

这种行为在C++标准中是否有任何说明?或者评估顺序是否未定义?

使用g ++ 5.2.1获得结果.

c++ operator-precedence language-lawyer

17
推荐指数
2
解决办法
2241
查看次数

Windows PowerShell 无法识别 sqlite3

我已经在我的电脑上安装了SQLite3,路径为G:\SQLite3\sqlite3.exe

但是,当我在 PowerShell 中输入“sqlite3”(不带引号)时,出现以下错误:

sqlite3 : The term 'sqlite3' is not recognized as the name of a cmdlet, functio
n, script file, or operable program. Check the spelling of the name, or if a pa
th was included, verify that the path is correct and try again.
At line:1 char:1
+ sqlite3
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (sqlite3:String) [], CommandNotF
   oundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我的环境路径包括G:\SQLite3,所以当我在命令提示符(cmd.exe)中运行sqlite3时,它运行得很好。不过,我更喜欢 PowerShell,所以如果有人能给我指出正确的方向,如何让它接受这个命令,我会很高兴。如果这很重要的话,我使用 Windows 8。

powershell

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

为什么以及何时通过指针传递C++中的类类型?

请考虑以下代码:

class Abstract{
public:
    virtual void printStuff()=0;
};

class Derived: public Abstract{
public:
    void printStuff(){
        printf("Stuff\n");
    }
};
Run Code Online (Sandbox Code Playgroud)

现在,假设我想创建一个使用Abstract类的printStuff方法的函数.在我了解到C++中只有一种方法可行之前,我认为有两种方法:指针不太明显,而且更明显,类似于你对int,chars等的期望:

void ptr_function(Abstract* abs){ //non-obvious
    abs->printStuff();
}

void non_ptr_function(Abstract abs){ //obvious, analogous to, say, pow(a,b)
    abs.printStuff();
}
Run Code Online (Sandbox Code Playgroud)

现在,我明白第二个是在C++中被禁止的.但是,我并不真正理解这种设计的根本原因.除了指针与作为参数传递的actal对象之外,上述函数看起来不一样吗?

作为一个后续问题:构建必须"包含"其他抽象类的类作为其中一个字段的首选方法是什么?如果这个问题的答案也是:"指针",那么我是否遗漏了某些东西,或者我是否必须自己跟踪这些物体的时间(即手动删除它们)?对于非抽象类,这不是问题,就好像我不使用指针一样,每当该对象超出范围时,它就会被自动删除(析构函数被调用等等).但是,如果我必须使用指针,看起来微管理会占用大量不必要的时间和代码.

有没有更好的方法来解决这个问题?

c++ oop pointers class

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

矩阵乘法速度问题

我正在调查缓存未命中如何影响计算速度.我知道有很多算法可以更好地将两个矩阵相乘(即使下面两个循环的简单交换也会有帮助),但请考虑以下代码:

float a[N][N];
float b[N][N];
float c[N][N];
// ...
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            float sum = 0.0;
            for (int k = 0; k < N; k++) {
                sum = sum + a[i][k] * b[k][j];
            }
            c[i][j] = sum;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经重新编译了这段代码,用于N运行它的许多值和测量时间.我预计会发现周围的时间突然增加,此时N=1250矩阵c不再适合缓存(c当时的大小1250*1250*sizeof(float)=6250000,或大约6MB,这是我的L3缓存的大小).

实际上,总体趋势是在此之后,平均时间与之前推断的时间相比大致为三倍.但价值N%8似乎对结果产生了巨大影响.例如:

1601 - 11.237548
1602 - 7.679103 …
Run Code Online (Sandbox Code Playgroud)

c++ performance caching matrix

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

如何禁用 avr-gcc 的“似乎是拼写错误的中断处理程序”警告?

我目前正在为 AVR 微控制器上的 USB 设备创建固件。由于 USB 时序非常严格,因此我不能允许非 USB 中断阻塞超过几个指令周期。因此,我的 USART RXC(接收到的字符)中断如下所示:

\n\n
void usart_rxc_wrapped() __attribute__ ((interrupt));\nvoid usart_rxc_wrapped(){\n    uint8_t c=UDR;\n    if(!ringBufferFull(&rx)){\n        ringBufferWrite(&rx, c);\n    }\n    // Reenable nterrupt\n    UCSRB|=1<<RXCIE;\n}\n\n// This cannot be ISR_NOBLOCK, since the interrupt would go\n// into infinite loop, since we wouldn\'t get up to reading\n// UDR register. Instead, we use assembly to do the job\n// manually and then jump to the real handler.\nISR(USART_RXC_vect, ISR_NAKED){\n    // Disable this interrupt by clearing its Interrupt Enable flag.\n    __asm__ volatile("cbi %0, %1"::\n            "I"(_SFR_IO_ADDR(UCSRB)),"I"(RXCIE));\n …
Run Code Online (Sandbox Code Playgroud)

c gcc avr

4
推荐指数
1
解决办法
1481
查看次数

C中的二维数组字符串

我在C中创建一个将使用文本菜单的应用程序.所以,我决定创建一个包含所有菜单的全局数组.作为稍后我将提到的问题的解决方法,代码如下所示:

char* main_menu[]=
{"Refresh rate", "Help", "Default config", NULL};
char* other_stuff[]=
{"Stuff", "More", NULL};

char** screens[]={main_menu, other_stuff};
Run Code Online (Sandbox Code Playgroud)

我可以使用这些字符串:screens[0][1]在第0个/第一个菜单中首先表示(或者说第二个,因为我们从零开始计算)选项.

它有效,但它让我觉得非常不优雅,我必须声明那些辅助阵列(main_menuother_stuff).我试图利用数组的嵌套大括号初始化,但总是编译器会抱怨.此外,在你建议将阵列声明为,比如说之前,char* screeens[10][5]应该没有神奇的数字,因为这对我来说同样不优雅.

我试过了:

char** screens[]={
{"Refresh rate", "Help", "Default config", NULL},
{"Stuff", "More", NULL}
};
Run Code Online (Sandbox Code Playgroud)

但是,编译器给了我一个警告(理所当然,因为访问元素会产生乱码):

../main.c:96:1: warning: braces around scalar initializer [enabled by default]
 {"Refresh rate", "Help", "Default config", NULL},
^
../main.c:96:1: warning: (near initialization for ‘screens[0]’) [enabled by default]
../main.c:96:1: warning: initialization from incompatible pointer type [enabled by default]
../main.c:96:1: warning: (near …
Run Code Online (Sandbox Code Playgroud)

c arrays

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

使用unique_ptr交换类

#include <memory>
#include <algorithm>

using namespace std;

class A {
public:
    unique_ptr<int> u;
    A(){}
    A(const A& other): u(new int(*other.u)){} // If I comment this out, it works.
    // A(A&&){} // This does not help.
};

int main() {
    A a;
    A b = a;
    swap(a, b);
}
Run Code Online (Sandbox Code Playgroud)

此代码不起作用 - 失败,出现模板错误no matching function for call to ‘swap(A&, A&)’.为什么?删除第二个构造函数会有所帮助,但我需要在其他代码中使用它.我猜测它可以与其他定义的一些构造函数的自动删除相关联,但手动添加移动构造函数也无济于事.我怎样才能解决这个问题?

c++ swap unique-ptr

0
推荐指数
1
解决办法
334
查看次数