小编klm*_*123的帖子

最快的无条件排序算法

我有一个函数,它可以采用两个元素并按升序返回:

void Sort2(int &a, int &b) { 
  if (a < b) return; 
  int t = a; 
  a = b; 
  b = t; 
}
Run Code Online (Sandbox Code Playgroud)

如果不允许使用额外的条件运算符,使用此函数对具有N个条目的数组进行排序的最快方法是什么? 这意味着我的整个程序应该如下所示:

int main(){
  int a[N];
     // fill a array

  const int NS = ...; // number of comparison, depending on N.
  const int c[NS] = { {0,1}, {0,2}, ... }; // consequence of indices pairs generated depending on N.
  for( int i = 0; i < NS; i++ ) {
    Sort2(a[c[i][0]], a[c[i][1]]);
  }
     // sort is …
Run Code Online (Sandbox Code Playgroud)

sorting algorithm

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

Checksum in C++

I need help with writing a code in C++ to do a 16 bit checksum.

The idea is to get data input from user (a string), convert it to binary form, then calculate checksum and put it in my data packet. I don't have any checksum type specification... I thought XORing the 16 bits would be a good idea. I have the following class packet given:

class packet
{
private:
    string message;
    unsigned int checksum;  // this is a 16 …
Run Code Online (Sandbox Code Playgroud)

c++ checksum

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

C++中最快的数组初始化

将整数数组初始化为给定值的最快方法是什么?

如果我有char数组,我可以使用memset:

char a[N];
memset( a, value, N );
Run Code Online (Sandbox Code Playgroud)

如果数组类型!= char和value!= 0或-1,我不能做memset:

int a[N];
memset( a, value, N*sizeof(int) ); // INCORRECT!
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有办法对其他数组类型进行类似的内存初始化.

c++ memory arrays c++03

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

编译连续函数调用的时间检查

假设我们有一个这样的类:

class OProcess {
  ...
  void Process1();
  void Process2(); // call only if Process1 wasn't called
  ...
}
Run Code Online (Sandbox Code Playgroud)

这样只有当函数Process1()尚未被调用时才能调用函数Process2().

有没有办法检查Process类在编译时是否正确使用?如果Process1()可以在Process2()之前为某些OProcess对象实例调用,则编译器必须给出错误.

PS我明白可以有这样的代码:

 if (variable == 1000)
   Process1();
 Process2();
Run Code Online (Sandbox Code Playgroud)

并且编译器无法确定在Process2()之前是否会调用Process1().但是编译器可以确保在Process2()之前可以为某些变量值调用Process1().我需要它来制造错误或至少警告.

c++ compiler-errors

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

NUMA:如何检查分配了 C++ 数组的 RAM 部分?

我有一台带有 2 个 CPU 和 64GB RAM、每个 CPU 32GB 的服务器。

我知道每个 CPU 都有自己的 RAM 部分,我们称它们为 RAM1 和 RAM2。我想让我的程序知道它在哪个 RAM(RAM1 或 RAM2)上分配数据。

我试图检查指针值:

  // put the thread at i-th CPU, using pthread_setaffinity_np
TData *a = new TData[N];
...
cout << "CPU = " << i << " adress = " << a << endl; 
Run Code Online (Sandbox Code Playgroud)

但输出看起来是随机的。我想那是因为地址是虚拟的。虚拟内存地址和部分内存有对应关系吗?

如何检查分配了我的数组“a”的哪个 RAM?

c++ multithreading virtual-memory numa

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

获得cout的精确度

我可以使用设置cout输出的精度

cout.precision(precision_value);
Run Code Online (Sandbox Code Playgroud)

如何获得cout已经使用的精度值?我在cout 描述中找不到任何其他相关功能.

我遇到的相关问题是如何仅为代码的某些部分更改cout精度(因此需要在之后重置为精确的先前值).

我试过:

//save original cout flags
std::ios_base::fmtflags coutFlags = cout.flags();

cout.precision(1);
cout.setf(ios::fixed);

 // code

cout.flags(coutFlags);
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

c++ precision cout

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

在存储库之间复制提交链

如何将提交链从一个 git(或 svn)存储库复制到另一个存储库?

如果我只有一次提交,我会这样做:

 cd Git1
 git diff commitHash^ commitHash > patch.txt
 cd Git2
 git branch CommitsFromGit1
 git checkout CommitsFromGit1
 patch -i ....Git1/patch.txt
 git ci -am "CommitFromGit1Name"
Run Code Online (Sandbox Code Playgroud)

但我的提交太多,无法一一手动进行。有没有办法创建某种带有一组提交更改的多补丁?

svn git version-control patch git-commit

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

为什么git stash导致冲突后git stash pop?

为什么在冲突结果git stash pop之后git stash呢?

所以我做了:

$ git stash --keep-index
$ git stash pop
Run Code Online (Sandbox Code Playgroud)

并且git要求我解决冲突.

我做错了吗?我怎么能不分级的藏文件(测试PROGRAMM),然后返回一切恢复(所以我已经得到了所有筹备的文件原封不动,未分级的文件回来,是不分阶段的)?

git git-stash

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

如何区分预处理器和编译器指令?

我被告知#pragma ompGCC中的指令是编译器的指令,并不是预处理器的指令.

这是对的吗?

如何区分预处理程序和编译器的指令?

c++ compiler-construction gcc openmp c-preprocessor

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

如何在 BEM 中使用伪类?

如何在遵循BEM 方法的同时有效地使用伪类?

我的具体示例如下 - 我需要创建一个具有自定义外观的复选框功能。为此,我通常会使用input:checked + label选择器。我正在尝试将其转换为 BEM,但我能做的最好的是:

CSS:

/*.custom-checkbox 
{
}*/

.custom-checkbox__input
{
    display: none;
}

.custom-checkbox__input:checked ~ .custom-checkbox__box-label
{   
    background-image: url("../../images/pro/check.png");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100%;
}

.custom-checkbox__box-label
{
    width: 1.4rem;
    height: 1.4rem;
    position: relative;
    top: 0.2rem;
    
    display: inline-block;
    background: white;
    border: 0px solid;
    border-radius: 0.2em;
}

.custom-checkbox__box-label:hover,
.custom-checkbox__txt-label:hover
{
    cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

   <li class="custom-checkbox">
     <input class="custom-checkbox__input" type="checkbox" id="distinctcat_cb1" checked>
     <label class="custom-checkbox__box-label" for="distinctcat_cb1"></label>
     <label class="custom-checkbox__txt-label" for="distinctcat_cb1">Categories</label>
   </li>
Run Code Online (Sandbox Code Playgroud)

由于兄弟选择器 ( ~),这与 BEM …

html css pseudo-class pseudo-element bem

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