小编Mar*_*ark的帖子

在迭代2D数组时,为什么循环的顺序会影响性能?

可能重复:
这两个for循环中的哪一个在时间和缓存性能方面更有效

下面是两个几乎相同的程序,除了我切换ij变量.它们都运行在不同的时间.有人能解释为什么会这样吗?

版本1

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

main () {
  int i,j;
  static int x[4000][4000];
  for (i = 0; i < 4000; i++) {
    for (j = 0; j < 4000; j++) {
      x[j][i] = i + j; }
  }
}
Run Code Online (Sandbox Code Playgroud)

版本2

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

main () {
  int i,j;
  static int x[4000][4000];
  for (j = 0; j < 4000; j++) {
     for (i = 0; i < 4000; i++) {
       x[j][i] = i …
Run Code Online (Sandbox Code Playgroud)

c optimization performance for-loop cpu-cache

350
推荐指数
6
解决办法
4万
查看次数

C++ ifstream错误使用字符串作为打开文件路径.

我有:

string filename: 
ifstream file(filename);
Run Code Online (Sandbox Code Playgroud)

编译器抱怨ifstream文件和字符串之间没有匹配.我需要将文件名转换为某些内容吗?

这是错误:

error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
Run Code Online (Sandbox Code Playgroud)

c++ ifstream

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

C++如何在堆栈上动态分配内存?

有没有办法在堆栈而不是堆上分配内存?我找不到一本好书,这里有人有个主意吗?

c++ memory memory-management

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

告诉cin停止阅读换行符

假设我想从输入中读取整数行a,如下所示:

1 2 3 4 5\n
Run Code Online (Sandbox Code Playgroud)

我希望cin停止'\n'字符,但cin似乎不认识它.

以下是我使用的内容.

vector<int> getclause() {
  char c;
  vector<int> cl;

  while ( cin >> c && c!='\n') {    
    cl.push_back(c);
    cin>>c;
  }
  return cl;
}
Run Code Online (Sandbox Code Playgroud)

我该如何修改它以便cin在看到'\n'字符时停止?

c++

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

&foo :: function和foo :: function有什么区别?

我在linux上使用gtkmm库为我的GUI绘制一个简单的菜单.

在下面的代码中,编译器抱怨无法解析地址

        sigc::mem_fun(*this, AppWindow::hide)));
                                         ^
appwindow.cpp:15:41: note:   could not resolve address from overloaded function
Run Code Online (Sandbox Code Playgroud)

但是当我插入&它时编译很好

m_menu_app.items().push_back(MenuElem("Quit",
    sigc::mem_fun(*this, &AppWindow::hide)));
Run Code Online (Sandbox Code Playgroud)

它在这方面有什么不同?这个hide功能首先不是一个地址吗?

c++ function-pointers

19
推荐指数
2
解决办法
872
查看次数

syscalls.h发生了什么事?

我正在测试K&R C书的一些代码示例,它#include "syscalls.h"在其中一个程序中使用,但编译器抱怨无法找到该文件.我该怎么替换syscalls.h?它被弃用了吗?

c

14
推荐指数
2
解决办法
9622
查看次数

C++使用函数作为参数

可能重复:
如何在C中将函数作为参数传递?

假设我有一个叫做的函数

void funct2(int a) {

}


void funct(int a, (void)(*funct2)(int a)) {

 ;


}
Run Code Online (Sandbox Code Playgroud)

调用此函数的正确方法是什么?我需要设置什么才能让它工作?

c++

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

检查无效的UTF8

我将从UTF8格式转换为十六进制的实际值.但是,我需要捕获一些无效的字节序列.有没有一种快速方法可以检查角色是否属于C++中的UTF8?

c++ utf-8

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

使用Emacs自动从.h插入原型函数

如何配置emacs以在打开相应的.cc文件时从.h自动插入原型函数?

c++ emacs

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

仅在有分隔符时动态显示MathJax

我一直在调整以下示例代码.MathJax的文档不是很完整.有人可以更多经验告诉我应该如何修改下面的代码,这样当我指定了像$\alpha $这样的分隔符时,Tex才会解析.我想让它像math.stackexchange一样工作.

   <html>
    <head>
    <title>MathJax Dynamic Math Test Page</title>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [["$","$"],["\\(","\\)"]]
        }
      });
    </script>
    <script type="text/javascript"
      src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full">
    </script>

    </head>
    <body>

    <script>
      //
      //  Use a closure to hide the local variables from the
      //  global namespace
      //
      (function () {
        var QUEUE = MathJax.Hub.queue;  // shorthand for the queue
        var math = null;                // the element jax for the math output.

        //
        //  Get the element jax when MathJax has produced it.
        //
        QUEUE.Push(function () …
Run Code Online (Sandbox Code Playgroud)

javascript mathjax

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