小编Aze*_*eem的帖子

为什么你会在C中的main()函数之前使用数据类型?

很多人都熟悉C语言中的hello world程序:

#include <stdio.h>

main ()
{
    printf ("hello world");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么有些在main()函数之前,int如下所示:

int main()
Run Code Online (Sandbox Code Playgroud)

另外,我已经看到void输入的内容()如下:

int main(void)
Run Code Online (Sandbox Code Playgroud)

这似乎是额外的打字,但也许这是一个在其他情况下支付红利的最佳做法?

另外,main()如果你要返回一个字符串,为什么会先加一个int?如果有的话,人们会期望:

char main(void)
Run Code Online (Sandbox Code Playgroud)

关于为什么我们在函数结束时返回0也很模糊.

c program-entry-point

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

在c ++中将std :: wstring转换为const*char

我怎么能转换std::wstringconst *char在C++?

c++

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

幕后的push_back()和emplace_back()

目前,我正在学习C++对我自己,和我很好奇如何push_back()emplace_back()引擎盖下工作.我总是认为emplace_back()当你试图构造并将一个大对象推到容器的后面时,如矢量更快.

让我们假设我有一个Student对象,我想要附加到学生矢量的背面.

struct Student {
   string name;
   int student_ID;
   double GPA;
   string favorite_food;
   string favorite_prof;
   int hours_slept;
   int birthyear;
   Student(string name_in, int ID_in, double GPA_in, string food_in, 
           string prof_in, int sleep_in, int birthyear_in) :
           /* initialize member variables */ { }
};
Run Code Online (Sandbox Code Playgroud)

假设我调用push_back()并将Student对象推送到向量的末尾:

vector<Student> vec;
vec.push_back(Student("Bob", 123456, 3.89, "pizza", "Smith", 7, 1997));
Run Code Online (Sandbox Code Playgroud)

我在这里的理解是在向量之外push_back创建Student对象的实例,然后将其移动到向量的背面.

图: https://ibb.co/hV6Jho

我也可以安抚而不是推动:

vector<Student> vec;
vec.emplace_back("Bob", 123456, 3.89, "pizza", "Smith", 7, 1997); …
Run Code Online (Sandbox Code Playgroud)

c++ vector std move-semantics c++11

12
推荐指数
1
解决办法
505
查看次数

使用remove_if从向量中删除元素

我试图删除矢量元素使用remove_if.但没有成功.我究竟做错了什么?

这是我的代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void printme(std::vector<int>& a){
    for(const auto& item: a)
    std::cout << item << std::endl;
}

int main()
{
    std::vector<int> a {1, 2, 3, 4, 5, 6};
    printme(a);  
    a.erase( (std::remove_if(a.begin(), a.end(), [](const int& x){
        return x == 2;
        }), a.end()));
    printme(a);
}
Run Code Online (Sandbox Code Playgroud)

我的输出只是:

1 2 3 4 5 6

预期产量:

1 2 3 4 5 6 1 3 4 5 6

c++ stl vector c++11

11
推荐指数
4
解决办法
1535
查看次数

GitHub Actions:如何将可重用工作流程作为步骤调用?

我有一个通过事件触发的主 GitHub Actions 工作流程workflow_dispatch,以及一个通过事件触发的可重用工作流程workflow_call

是否可以通过主工作流程中的步骤而不是作为作业来运行可重用工作流程?

主要工作流程:

name: main-workflow
on:
  workflow_dispatch:
    inputs:
      message:
       type: string
       description: 'A message to pass to reusable workflow'

jobs:
  use-reusable-workflow:
    runs-on: ubuntu-latest
    steps:
      - name: Call reusable workflow
        uses: my-org/reusable-workflow@main
        with:
          workflow: reusable-workflow.yml
          inputs: |
            message=${{ github.event.inputs.message }}
          secrets: |
            A_SECRET: ${{ secrets.A_SECRET }}
Run Code Online (Sandbox Code Playgroud)

运行主工作流程时,我收到以下错误:

错误:在以下位置找不到“action.yml”、“action.yaml”或“Dockerfile”

github github-actions

11
推荐指数
1
解决办法
9387
查看次数

_itoa和itoa有什么区别?

Visual Studio对我说使用itoa()说法来_itoa()代替?

在我看来,它们是相同的功能.是什么赋予了?

c c++ itoa visual-studio

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

修改c ++字符串对象的底层char数组

我的代码是这样的:

string s = "abc";
char* pc = const_cast<char*>( s.c_str() );
pc[ 1 ] = 'x';
cout << s << endl;
Run Code Online (Sandbox Code Playgroud)

当我使用GCC编译上面的代码片段时,我得到了结果"axc",如预期的那样.我的问题是,char以这种方式修改C++字符串的底层数组是否安全且可移植?或者可能有其他方法直接操作字符串的数据?

仅供参考,我的目的是编写一些可以由C和C++调用的纯C函数,因此,它们只能char*作为参数接受.从char*字符串到字符串,我知道涉及复制,惩罚是不利的.所以,任何人都可以提出一些建议来处理这种情况.

c++ arrays string

10
推荐指数
3
解决办法
4388
查看次数

crudRepository的findAll()方法返回null值

我正在使用Spring数据Redis并拥有以下存储库:

public interface MyClassRepository extends CrudRepository<MyClass, String> {
}
Run Code Online (Sandbox Code Playgroud)

当我调用findAll(Iterable< String> ids)方法时,返回正确的数据:

final List<String> ids = Lists.newArrayList("id1", "id2");
final Iterable<MyClass> mappingIterable = mappingRepository.findAll(ids);
Run Code Online (Sandbox Code Playgroud)

但是,调用findAll()不返回数据,而是返回Redis null中每个id当前值的值:

final Iterable<MyClass> mappingIterable = mappingRepository.findAll();
Run Code Online (Sandbox Code Playgroud)

返回:

[null,null]

java crud spring-data-redis

10
推荐指数
1
解决办法
1134
查看次数

QPainter :: save()和QPainter :: restore()有什么作用?

我不能肯定它是什么,QPainter确实,当我调用save()restore().

难道节省它绘制的图像,或只是保存等有关的信息penWidthcolor等?
我可以用它来恢复上一个油漆事件的图像吗?

qt

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

当我启动一个新线程时为什么Valgrind会出现段错误

我正在用C++编写一个程序,我注意到一些非常奇怪的东西.

当我在Xcode下运行程序时,一切正常,但是当我在Valgrind下执行时,它会segmentation fault在几秒钟后给我一个.

我设法提取了一个非常简单的代码,它给了我这个错误:

#include <thread>

void exec_1() {}

int main(int argc, const char * argv[]) {

    std::thread simulator_thread;
    simulator_thread = std::thread(exec_1);
    simulator_thread.join();

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

我正在做的只是使用这些标志在Xcode下构建我的可执行文件:

CFLAGS:

-I/usr/local/lib/python3.6/site-packages/numpy/core/include
-I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m 
-Wno-unused-result -Wsign-compare -Wunreachable-code
-fno-common -dynamic -DNDEBUG -g -fwrapv -Wall -Wstrict-prototypes
Run Code Online (Sandbox Code Playgroud)

LDFLAGS:

-L/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin
-lpython3.6m -ldl -framework CoreFoundation
Run Code Online (Sandbox Code Playgroud)

然后在Valgrind下运行可执行文件以查找内存泄漏.你会看到我正在调用它,Python C API因为我在我的main代码中使用它但是这段代码却抛弃了我segfault而不使用它们.

无论如何Valgrind,以及其他一些东西,给我以下输出:

Thread 2:
==41660== Invalid read of size 4
==41660==    at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading valgrind segmentation-fault

9
推荐指数
1
解决办法
1513
查看次数