小编mah*_*ood的帖子

在python中连接字符串

我正在将命令行转换为python字符串.命令行是:

../src/clus -INFILE=../input/tua40.sq -OUTPUT=OUT
Run Code Online (Sandbox Code Playgroud)

python语句是:

c_dir = '~/prj/clus/'
c_bin = c_dir + 'src/clus'
c_data = c_dir + 'input/tua40.sq'

c = LiveProcess()
c.executable = c_bin
c.cwd = c_dir 
c.cmd = [c.executable] + ['-INFILE=', 'c_data, '-OUTPUT=OUT'] 
Run Code Online (Sandbox Code Playgroud)

问题是c.cmd最后的样子

~/prj/clus/src/clus -INFILE= ~/prj/clus/input/tua40.sq ...
Run Code Online (Sandbox Code Playgroud)

并非'='后面有一个"空格",导致程序报告错误.

如何将'='连接到路径?

python arrays string concatenation

0
推荐指数
1
解决办法
473
查看次数

重命名一组文件

我想将具有此模式的所有文件重命名-512--256-文件夹中?我怎么能用mv做到这一点?我知道我必须使用xargs并管道它,find但不知道如何告诉xargs

find . -name *-512-*
Run Code Online (Sandbox Code Playgroud)

xargs mv ?????
Run Code Online (Sandbox Code Playgroud)

bash xargs mv

0
推荐指数
1
解决办法
187
查看次数

在unordered_map中插入元素会产生错误

我已经定义了unordered_map这样的

struct pht {
  pht(int t, vector<bool> pat) 
: tag(t), pattern(32) {}
private:
  int tag;
  vector<bool> pattern;
};

unordered_map< pair<int, int>, pht > predictor;

int main()
{
  int pc, addr, offset, tag;
  vector<bool> pat;
  srand(time(0));

  tag = 1000; pc = 100; offset = 10; 
  for ( int i = 0; i < 32; i++ ) 
  pat.push_back( rand() % 2 );
  predictor.insert(make_pair( make_pair(pc, offset), pht(tag, pat) ) );
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是我收到此错误:

(更新)

error C2440: 'type cast' : cannot …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map vector

0
推荐指数
1
解决办法
925
查看次数

初始化类型的引用无效

isInstruction()我的代码(不是我的)中有一个函数,用于设置和获取成员没有问题.现在我为了类似的目的添加了我自己的功能state().像这样:

struct foo {
  bool & isInstruction() {
    return isInst;    // no problem
  }
  int & state() {
    return state;   //ERROR
  } 
private:
  bool isInst;
  int state;  
};
Run Code Online (Sandbox Code Playgroud)

我对第一个函数没有问题.但是对于第二个,我得到了

error: invalid initialization of reference of type ‘int&’ from expression of type 
‘<unresolved overloaded function type>’
Run Code Online (Sandbox Code Playgroud)

那么问题是这两个函数之间的区别是什么.我错过了什么吗?

c++

0
推荐指数
1
解决办法
1634
查看次数

构建混合的c/c ++代码

我有一个.cc文件,使用iostreammalloc.我怎么编译呢?使用g++,它说

 error: 'malloc' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

使用gcc,它说

 fatal error: iostream: No such file or directory
Run Code Online (Sandbox Code Playgroud)

源代码位于http://sequitur.info/sequitur_simple.cc

UPDATE

我改变mallocnew并且去freedelete.我仍然遇到很多错误.例如

 /usr/include/c++/4.6/new:103:14: error:   initializing argument 2 of âvoid* operator new(std::size_t, void*)â [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

c c++

0
推荐指数
1
解决办法
175
查看次数

undefined引用`tan'但是包含了math.h

我想从http://pages.cs.wisc.edu/~travitch/pthreads_primer.html(Mutex部分)编译示例pthread代码.当我运行此命令时:

 gcc -pedantic -Wall -o theaded_program pth.c -lpthread 
Run Code Online (Sandbox Code Playgroud)

在链接中说明,我收到此错误

pth.c:45:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
/tmp/ccajksBv.o: In function `opponent':
pth.c:(.text+0x4a): undefined reference to `tan'
/tmp/ccajksBv.o: In function `main':
pth.c:(.text+0x131): undefined reference to `tan'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但是#include <math.h>代码中有!! gcc版本是4.6

math gcc

0
推荐指数
1
解决办法
6203
查看次数

比较两个向量,使其更有效

使用kcachegrind并在调试模式下运行代码,我发现我的程序的瓶颈是两个向量进行比较的点.

if (v1 == v2) {
  // DO
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能提高效率?这是否更好

if (v1[0] == v2[0]) {
   if (v1 == v2) {
      // DO
   }
}
Run Code Online (Sandbox Code Playgroud)

第一行将过滤一些无用的比较.

在此之前我尝试过

if (!v2.empty())
  if (v1 == v2) 
   // DO
Run Code Online (Sandbox Code Playgroud)

但是我发现他们几乎总是不空.因此还包括空()的额外时间.

我不得不说矢量的大小很小.2~4个元素.在极少数情况下,它们将延伸到10.

更新: 感谢Mats Petersson,似乎通过在优化模式下进行编译,可以提高性能.

c++ performance vector

0
推荐指数
1
解决办法
231
查看次数

将assert()与消息一起使用

是否有可能在断言错误上写一条消息?

例如,使用#include <assert.h>,我只能写:

void foo(int a, int b) {
  assert (a != b);
}
Run Code Online (Sandbox Code Playgroud)

然而,在一个错误我想看到的价值ab.我怎样才能做到这一点?

c c++ assert

0
推荐指数
2
解决办法
2157
查看次数

在std :: list和std :: vector之间进行选择

我写了一个代码,试图在向量中找到重复.如果重复,它会将位置添加到列表中.例如,序列100 110 90 100 140 90 100将是2D矢量.第一维包含唯一的字母(或数字),并且重复列表作为第二维附加.所以结果看起来像

100 -> [0 3 6]
110 -> [1]
90 -> [2 5]
140 -> [4]
Run Code Online (Sandbox Code Playgroud)

代码非常简单

typedef unsigned long long int ulong;
typedef std::vector<ulong> aVector;
struct entry {
  entry( ulong a, ulong t ) {
    addr = a;
    time.push_back(t);
  }
  ulong addr;
  aVector time;
};

// vec contains original data
// theVec is the output vector
void compress( aVector &vec, std::vector< entry > &theVec )
{
   aVector::iterator it = vec.begin();
   aVector::iterator …
Run Code Online (Sandbox Code Playgroud)

c++ list vector

0
推荐指数
1
解决办法
189
查看次数

std :: ofstrean不能与-O3一起使用

以下代码,简单地说

1)从命令行参数中获取输入文件名,比如in.txt

2)将文件名附加到"cdf_"

3)打开一个名为cdf_in.txt的文件

4)简单地从(每行中的一个数字)读取每一行并将其发送到输出文件.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main(int argc, char* argv[]) 
{
  char *ben = argv[1];    // example: in.txt
  ifstream fin (ben);
  char res[30];

  char *o1 = "cdf_";
  strcat(res, o1);
  strcat(res, ben);
  ofstream fout (res, std::ofstream::out);   // will be cdf_in.txt
  cout << res << endl;

  uint64_t num;  uint64_t sum = 0;
  while (fin >> num) {
    fout << num << endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

通过运行程序而不进行任何优化,它可以正常运行.但是,通过指定-O3,它无法创建输出文件.为什么???

$ g++ -o cdf …
Run Code Online (Sandbox Code Playgroud)

c++ fstream

0
推荐指数
1
解决办法
85
查看次数

标签 统计

c++ ×7

vector ×3

c ×2

arrays ×1

assert ×1

bash ×1

concatenation ×1

fstream ×1

gcc ×1

list ×1

math ×1

mv ×1

performance ×1

python ×1

string ×1

unordered-map ×1

xargs ×1