天道酬勤,学无止境

c++

重新定义 C++ 中的基本类型(Redefine basic types in C++)

问题 是否可以重新定义 C++ 中的int是什么? 原因是因为我想在计算机上模拟一些 Arduino 代码,而不更改 Arduino 代码。 例如,为了以同样的方式演示溢出, int需要是 16 位而不是我机器上的 32 位。 所以基本上我想: typedef int16_t int; 以及对 Arduino 使用的其他数据类型执行相同的操作。 但是我认为这是不可能的,因为它会引发错误(不足为奇): error: cannot combine with previous 'type-name' declaration specifier 有没有办法可以轻松做到这一点? 回答1 您不能重新定义int但您可以停止使用int编写代码并使用类型别名,例如: typedef int int_type; int main() { int_type i = 0; // etc... } 然后您可以根据需要切换类型: typedef int16_t int_type; // now using 16 bit integers

2022-01-25 05:39:42    分类:技术分享    c++   typedef

为什么我的快速排序算法列表中有一个 0 浮点数以及如何将 NaN 包含到我的快速排序算法中?(Why is there a 0 floating point in my Quicksort algorithm list and how to include a NaN into my Quicksort algorithm?)

问题 以下是我的代码: #include <stdlib.h> #include <stdio.h> #include <limits> #define INFINITY std::numeric_limits<float>::infinity() #define NEGINFINITY -std::numeric_limits<float>::infinity() int floatcomp(const void* elem1, const void* elem2) { if (*(const float*)elem1 < *(const float*)elem2) return -1; return *(const float*)elem1 > *(const float*)elem2; } int main() { float array[10] = {INFINITY, 3.5f, 144.4f, NAN, 12.4f, NEGINFINITY, 1.4f, -0.0f, 5.9f}; int i; for (i = 0; i < 10; i++) printf("%f\n", array[i]); printf("\n"); qsort(array, 10, sizeof(float), floatcomp); for (i = 0; i < 10; i++)

2022-01-25 05:38:06    分类:技术分享    c++   algorithm   quicksort   nan

二维数组(矩阵)内存分配的烦恼(Two-dimensional array (matrix) memory allocation troubles)

问题 我有几行 C++ 代码,其目的很简单 - 将矩阵存储在二维数组中,并且不会消耗比所需更多的内存。 这意味着在将矩阵放入矩阵之前,我必须为矩阵中的每个数字分配内存。 #include <cstdlib> #include <cstdio> using namespace std; int main() { int ** matrix; matrix = (int**)malloc(sizeof(int*)); // Fill in the matrix matrix[0] = (int *) malloc(3 * sizeof(int)); matrix[0][0] = 5; matrix[0][1] = 10; matrix[0][2] = 15; matrix[1] = (int *) malloc(3 * sizeof(int)); matrix[1][0] = 2; matrix[1][1] = 4; matrix[1][2] = 6; int i, n; // Print the whole matrix for (n = 0; n < 3; n++) { for (i = 0; i < 3; i++) { printf("%i\t", matrix[n][i]); } printf("\n"); } return 0; } 当我编译上面的代码并运行它时

2022-01-25 05:37:23    分类:技术分享    c++   arrays   memory-management

How to determine if my GPU does 16/32/64 bit arithmetic operations?

I am trying find the throughput of native arithmetic operations on my Nvidia card. On this page, Nvidia have documented the throughput values for various arithmetic operations. The problem is how do I determine if my card does 16 or 32 or 64 bit operations, since the values are different for each? Further, I also want to calculate the latency values of these instructions for my card. Is there some way to do it? As far as my research goes, they are not documented like throughput. Is there some benchmark suite for this purpose? Thanks!

2022-01-25 05:35:52    分类:问答    c++   cuda   nvidia   latency

将纪元转换为 time_t(convert epoch to time_t)

问题 我有一个双倍的时代,需要传递给一个需要 time_t 的函数。 如何将双倍的纪元转换为 time_t? 我在使用 C++ 代码的 Linux 上。 谢谢。 回答1

2022-01-25 05:32:46    分类:技术分享    c++   epoch

Errors while using templates templates parameters

I am studying CPP templates myself and got stuck while trying templates of templates parameters for a class. I am getting errors when I am trying to instantiate the class member. #pragma once #include "stdafx.h" # include <list> template<class type, template<type> class T> class stack { private: int count; int size; T<type> st; public: stack(size_t size):size(100), count(-1){ } void push(type elem); type pop(); }; template<class type, template<type> class T> void stack<type, T>::push(type elem) { if (count < (size - 1)) { st.push_back(elem); count++; } else { cout << "Elements cannot be added

2022-01-25 05:31:39    分类:问答    c++   templates   stl   template-templates

How do I use libraries / includes in C++?

Is there a difference between #include and libraries? Or can you just include libraries in your application? Another thing I want to know is if I want to distribute my application how and where do I put the files that I've included in my program? Am I allowed to put the libraries and header files in the same folder as the .cpp files? This confusion arose when I was trying to figure out how to install the SFML library into Dev-C++, now before you tell me that Dev is old and I should change, I won't because it works fine for me. I don't like Visual because it's a little disorienting, I just want

2022-01-25 05:29:31    分类:问答    c++   include   libraries   dev-c++

An executable and a shared library dependent on a same statically linked library

Suppose you're developing a shared library libshared.so. And you have a static library libstatic.a with some internal classes and functionality you need. You'd like to link it to your .so like this: g++ -o libshared.so -shared myObj.o -lstatic Also you have an executable.sh which will use your .so and dynamically open it in the runtime dlopen("libshared.so", RTLD_NOW) You know this executable was as well statically linked against libstatic.a (but you're not sure the version of the library is exactly the same as yours). So the question is: Is it safe and correct to statically link your

2022-01-25 05:28:53    分类:问答    c++   linux   shared-libraries   static-libraries   linkage

How do I use the generic error codes enum with system_category error codes from <system_error>?

I have this code (very similar to what is suggested here) that throws an exception: int accept4(int sockfd, sockaddr *addr, socklen_t *addrlen, int flags) { const int fd = ::accept4(sockfd, addr, addrlen, flags); if (fd < 0) { const auto tmp = errno; throw ::std::system_error(tmp, ::std::system_category(), "accept4(2)"); } else { return fd; } } And this code for testing for a particular exception reason: catch (const ::std::system_error &e) { static const auto block_err = ::std::system_error(EWOULDBLOCK, ::std::system_category()); const auto ecode = e.code(); // EWOULBLOCK is fine, everything

2022-01-25 05:28:42    分类:问答    c++   c++11   exception-handling