小编Tre*_*key的帖子

C和C++中的静态和外部全局变量

我做了2个项目,第一个用C语言,第二个用C++编写,两个项目都有相同的行为.

C项目:

header.h

int varGlobal=7;
Run Code Online (Sandbox Code Playgroud)

main.c中

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

void function(int i)
{
    static int a=0;
    a++;
    int t=i;
    i=varGlobal;
    varGlobal=t;
    printf("Call #%d:\ni=%d\nvarGlobal=%d\n\n",a,i,varGlobal,t);
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C++项目:

header.h

int varGlobal=7;
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include "header.h"
using namespace std;

void function(int i)
{
    static int a=0;
    int t=i;
    a++;
    i=varGlobal;
    varGlobal=t;
    cout<<"Call #"<<a<<":"<<endl<<"i="<<i<<endl<<"varGlobal="<<varGlobal<<endl<<endl; 
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我读到默认情况下全局变量是extern,在C++中默认是C和static ; 那么为什么C++代码有效呢?

我的意思是int …

c c++ static global-variables extern

26
推荐指数
2
解决办法
9万
查看次数

Makefile(自动依赖生成)

只是为了快速术语:

#basic makefile rule
target: dependencies
    recipe
Run Code Online (Sandbox Code Playgroud)

问题:我想自动生成依赖项.

例如,我希望转此:

#one of my targets
file.o: file.cpp 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h another.h lots.h evenMore.h
    $(COMPILE)
Run Code Online (Sandbox Code Playgroud)

进入:

#one of my targets
file.o: $(GENERATE)
    $(COMPILE)
Run Code Online (Sandbox Code Playgroud)

而且我不太确定它是否可能......

我所知道的:

我可以使用这个编译器标志:

g++ -MM file.cpp
Run Code Online (Sandbox Code Playgroud)

它将返回正确的目标和依赖.
所以从示例中,它将返回:

file.o: file.cpp 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h another.h lots.h evenMore.h  
Run Code Online (Sandbox Code Playgroud)

但是,'make'不允许我在规则的目标或依赖部分中显式编写shell代码:(
我知道有一个'make'函数叫做shell

但我不能完全插入这个依赖并解析魔法,因为它依赖于表示目标的宏$ @或者至少我认为这就是问题所在

我甚至尝试用这个makefile函数替换"file.cpp"依赖项,但这也无效.

#it's suppose to turn the $@ (file.o) into file.cpp
THE_CPP := $(addsuffix $(.cpp),$(basename $@))

#one of my targets …
Run Code Online (Sandbox Code Playgroud)

dependencies gnu makefile

25
推荐指数
4
解决办法
2万
查看次数

我怎样才能只应用一个clang格式的动作?

我想使用clang-format来对齐我的评论,但没有别的.

选项是: AlignTrailingComments (bool).

但是当我运行以下内容时:

clang-format-3.6 -i -style='{AlignTrailingComments: true}' <file>
Run Code Online (Sandbox Code Playgroud)

它执行各种其他格式化选项,我想在未指定时具有默认值.

如何在代码库上只执行一个clang格式化规则?

拥有所有这些默认值后,很难看到单个格式选项对代码的完整影响.我必须解析所有这些其他更改的差异,并确定它是否是我指定实际执行的选项.


我注意到有一个DisableFormat选项,但无论我如何使用它,它都会阻止任何格式化.

clang-format-3.6 -i -style='{AlignTrailingComments: true, DisableFormat: true}'

clang-format-3.6 -i -style='{DisableFormat: true, AlignTrailingComments: true}'
Run Code Online (Sandbox Code Playgroud)

两者都导致clang-format不能在任何地方生成任何代码.

c++ code-formatting clang-format

25
推荐指数
2
解决办法
1108
查看次数

为什么std :: bitset没有迭代器?

似乎std :: bitset没有STL迭代器.
因此,我不能做到以下几点:

std::bitset<8> bs;
for (auto it: bs) {
    std::cout << "this can not be done out of the box\n";
}
Run Code Online (Sandbox Code Playgroud)

相反,我必须:

std::bitset<8> bs;
for (std::size_t i = 0; i < bs.size(); ++i) {
    std::cout << bs[i] << '\n';
}
Run Code Online (Sandbox Code Playgroud)

没有迭代器,我也不能将bitset与任何STL算法一起使用.
为什么委员会决定从bitset中排除迭代器?

c++ iterator stl bitset std-bitset

24
推荐指数
1
解决办法
5444
查看次数

查找所有子字符串的出现次数和位置

我正在编写一个程序来解析保存为文本文件的一些数据.我想要做的是找到大海捞针中每根针的位置.我已经可以读取文件并确定出现次数,但我也希望找到索引.

c++ iostream stdio

23
推荐指数
2
解决办法
3万
查看次数

有没有办法通过引用传递,并在函数调用中显式传递值?

如果你要查看这段代码,

int x = 0;
function(x);
std::cout << x << '\n';
Run Code Online (Sandbox Code Playgroud)

您将无法通过任何语法方式验证参数x是通过引用传递还是通过值传递.您可以肯定的唯一方法是查看函数声明或函数定义.

这是一个简单的例子,我认为这可能是一个问题:

std::string Lowercase(std::string str); //<- this is hidden away in code; probably in a different file.

int main(){
    std::string str = "HELLO";
    Lowercase(str);
    std::cout << str << '\n'; //<- Bug! we expected to output "hello".  The problem is not very easy to spot, especially when a function name sounds as though it will change the passed in value.
}
Run Code Online (Sandbox Code Playgroud)

为了避免必须在函数调用和函数声明(或在某些情况下,文档)之间跳转以理解函数行为,有没有办法在函数调用的语法中显式地记录参数是期望改变(即参考参数)或发送副本(即通过值传递)?

我意识到还有一个选择传递const&,它具有类似于传递值的概念,因为传入的变量在函数调用之后不会更改其值.


我确信语言中有各种各样的情况可能会增加理解参数传递的复杂性 - 但我很好奇,有没有办法以我想要的方式解决这个问题? …

c c++ parameters parameter-passing c++11

23
推荐指数
2
解决办法
1491
查看次数

在Python中使用类对函数进行分组

我已经成为Python科学程序员已有几年了,随着程序越来越大,我发现自己遇到了一个特定的问题.我是自学成才,因此我从未接受过任何正式的培训,并且花了很多时间在"适当"的Python编码"惯例"上.

无论如何,到目前为止,我发现自己总是创建一个utils.py文件,我将所有已定义的函数存储在我的程序使用的文件中.然后我发现自己将这些功能分组到各自的目的中.我知道分组事物的一种方式当然是使用类,但我不确定我的策略是否违背实际应该使用的类.

假设我有一堆功能大致相同的事情:

def add(a,b):
    return a + b

def sub(a,b):
    return a -b

def cap(string):
    return string.title()

def lower(string):
    return string.lower()
Run Code Online (Sandbox Code Playgroud)

现在显然这4个函数可以看作是两个单独的目的,一个是计算,另一个是格式化.这就是逻辑告诉我要做的事情,但我必须解决它,因为我不想初始化对应于类evertime的变量.

class calc_funcs(object):

    def __init__(self):
        pass

    @staticmethod
    def add(a,b):
        return a + b

    @staticmethod
    def sub(a, b):
        return a - b

class format_funcs(object):
    def __init__(self):
        pass

    @staticmethod
    def cap(string):
        return string.title()

    @staticmethod
    def lower(string):
        return string.lower()
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我现在将这些方法"组合"成一个很好的包,可以根据它们在程序中的角色更快地找到所需的方法.

print calc_funcs.add(1,2)
print format_funcs.lower("Hello Bob")
Run Code Online (Sandbox Code Playgroud)

然而,话虽如此,我觉得这是一种非常'unpython-y'的做事方式,它只是感觉凌乱.我是想以正确的方式思考这个还是有另一种方法?

python class conventions

23
推荐指数
2
解决办法
6144
查看次数

在Javascript中隐藏按钮

在我的最新程序中,有一个按钮,单击时会显示一些输入弹出框.这些盒子消失后,我该如何隐藏按钮?

javascript html5 button hide

22
推荐指数
3
解决办法
20万
查看次数

bool保证是1个字节吗?

锈病文档是模糊bool的大小.
它保证是1个字节,还是像C++一样未指定?

fn main() {
    use std::mem;
    println!("{}",mem::size_of::<bool>()); //always 1?
}
Run Code Online (Sandbox Code Playgroud)

size primitive-types rust

22
推荐指数
2
解决办法
1990
查看次数

访问静态超出范围的未定义行为?

在与我的一位同事交谈时,他们说:

foo() {
    int *p;
    {
        int x = 5;
        p = &x;
    }
    int y = *p;
}
Run Code Online (Sandbox Code Playgroud)

创建未定义的行为,因为生命周期规则和范围规则未指定.

然而:

foo() {
    int *p;
    {
        static int x = 5;
        p = &x;
    }
    int y = *p;
}
Run Code Online (Sandbox Code Playgroud)

没有定义!您最终会遇到"间接范围"问题.

术语的使用听起来不正确.
我知道静态与范围无关.
第二种情况是否定义了行为?

c++ static scope lifetime undefined-behavior

21
推荐指数
1
解决办法
1017
查看次数