我是C++和STL的新手.我坚持使用存储自定义数据结构的哈希集的以下简单示例:
#include <iostream>
#include <ext/hash_set>
using namespace std;
using namespace __gnu_cxx;
struct trip {
int trip_id;
int delta_n;
int delta_secs;
trip(int trip_id, int delta_n, int delta_secs){
this->trip_id = trip_id;
this->delta_n = delta_n;
this->delta_secs = delta_secs;
}
};
struct hash_trip
{
size_t operator()(const trip t)
{
hash<int> H;
return H(t.trip_id);
}
};
struct eq_trip
{
bool operator()(const trip t1, const trip t2) {
return (t1.trip_id==t2.trip_id) &&
(t1.delta_n==t2.delta_n) &&
(t1.delta_secs==t2.delta_secs);
}
};
int main()
{
hash_set<trip, hash_trip, eq_trip> trips;
trip t = …Run Code Online (Sandbox Code Playgroud) C++中的引用是一个convneint结构,允许我们简化以下C代码:
f(object *p){
//do something
}
int main(){
object* p = (object*) calloc(sizeof(object));
f(p);
}
Run Code Online (Sandbox Code Playgroud)
至
f(object& o){
//do something
}
int main(){
object o = object();
f(o);
}
Run Code Online (Sandbox Code Playgroud)
共享指针是C++中另一种简化内存管理的便利.但是,我不知道如何将shared_ptr一个函数传递给一个f(object& o)通过引用接受参数的函数?
f(object& o){
//do something
}
int main(){
shared_ptr<object> p (new object());
f(*p);
}
Run Code Online (Sandbox Code Playgroud)
当通过引用函数传递其对象时,共享指针是否会递增?
我正在写一个似乎在泄漏内存的python扩展.我试图用valgrind找出问题的根源.
然而,根据valgrind,似乎python本身正在泄漏内存.使用以下简单脚本:
hello.py
print "Hello World!"
Run Code Online (Sandbox Code Playgroud)
并做
> valgrind --tool=memcheck python ./hello.py
(...)
==7937== ERROR SUMMARY: 580 errors from 34 contexts (suppressed: 21 from 1)
==7937== malloc/free: in use at exit: 721,878 bytes in 190 blocks.
==7937== malloc/free: 2,436 allocs, 2,246 frees, 1,863,631 bytes allocated.
==7937== For counts of detected errors, rerun with: -v
==7937== Use --track-origins=yes to see where uninitialised values come from
==7937== searching for pointers to 190 not-freed blocks.
==7937== checked 965,952 bytes.
==7937==
==7937== LEAK SUMMARY: …Run Code Online (Sandbox Code Playgroud) 我有以下包含O(N)元素的稀疏矩阵
boost::numeric::ublas::compressed_matrix<int> adjacency (N, N);
Run Code Online (Sandbox Code Playgroud)
我可以编写一个强力双循环来及时检查所有条目,O(N^2)如下所示,但这将太慢.
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
std::cout << adjacency(i,j) std::endl;
Run Code Online (Sandbox Code Playgroud)
如何及时循环非零条目O(N)?对于每个非零元素,我希望能够访问其值和索引i,j.
我有以下代码:
#include <iostream>
#include "boost/unordered_map.hpp"
using namespace std;
using namespace boost;
int main()
{
typedef unordered_map<int, int> Map;
typedef Map::const_iterator It;
Map m;
m[11] = 0;
m[0] = 1;
m[21] = 2;
for (It it (m.begin()); it!=m.end(); ++it)
cout << it->first << " " << it->second << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我正在寻找保留顺序的东西,以便稍后我可以按照它们插入的相同顺序迭代元素.在我的计算机上,上面的代码不保留订单,并打印以下内容:
0 1
11 0
21 2
Run Code Online (Sandbox Code Playgroud)
我想也许我可以用一个 boost::multi_index_container
typedef multi_index_container<
int,
indexed_by<
hashed_unique<identity<int> >,
sequenced<>
>
> Map;
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何使用这个容器(或任何其他适当的容器)实现我的原始代码,以便迭代器遵循插入的顺序?
我正在尝试使用带有gobject内省的pygi创建到vala库的python绑定.但是,我在生成GIR文件时遇到了问题(我计划随后将其编译为typelib文件).根据文档,valac应该支持生成GIR文件.
编译以下内容
helloworld.vala
public struct Point {
public double x;
public double y;
}
public class Person {
public int age = 32;
public Person(int age) {
this.age = age;
}
}
public int main() {
var p = Point() { x=0.0, y=0.1 };
stdout.printf("%f %f\n", p.x, p.y);
var per = new Person(22);
stdout.printf("%d\n", per.age);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用命令
valac helloworld.vala --gir=Hello-1.0.gir
Run Code Online (Sandbox Code Playgroud)
不像Hello-1.0.gir人们期望的那样创建文件.如何生成gir文件?
std::tie 提供了一种方便的方法,可以将C++中元组的内容解压缩为单独定义的变量,如下面的示例所示
int a, b, c, d, e, f;
auto tup1 = std::make_tuple(1, 2, 3);
std::tie(a, b, c) = tup1;
Run Code Online (Sandbox Code Playgroud)
但是,如果我们有一个像下面这样的嵌套元组
auto tup2 = std::make_tuple(1, 2, 3, std::make_tuple(4, 5, 6));
Run Code Online (Sandbox Code Playgroud)
试图编译代码
std::tie(a, b, c, std::tie(d, e, f)) = tup2;
Run Code Online (Sandbox Code Playgroud)
失败并出错
/tmp/tuple.cpp:10: error: invalid initialization of non-const reference of type ‘std::tuple<int&, int&, int&>&’ from an rvalue of type ‘std::tuple<int&, int&, int&>’
std::tie(a, b, c, std::tie(d, e, f)) = tup2;
^
Run Code Online (Sandbox Code Playgroud)
有没有一种惯用的方法来解压缩C++中的元组元组?
即使一切似乎都是矢量化的,下面的代码运行得太慢了.
from numpy import *
from scipy.sparse import *
n = 100000;
i = xrange(n); j = xrange(n);
data = ones(n);
A=csr_matrix((data,(i,j)));
x = A[i,j]
Run Code Online (Sandbox Code Playgroud)
问题似乎是索引操作是作为python函数实现的,并且调用A[i,j]结果导致以下分析输出
500033 function calls in 8.718 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100000 7.933 0.000 8.156 0.000 csr.py:265(_get_single_element)
1 0.271 0.271 8.705 8.705 csr.py:177(__getitem__)
(...)
Run Code Online (Sandbox Code Playgroud)
也就是说,python函数_get_single_element被调用100000次,这实在是效率低下.为什么不在纯C中实现?有没有人知道解决这个限制的方法,并加快上述代码?我应该使用不同的稀疏矩阵类型吗?
我试图从pysparse库中添加一些额外的方法到矩阵类型.除此之外,我希望新类的行为与原始类完全相同,因此我选择使用继承来实现更改.但是,当我尝试
from pysparse import spmatrix
class ll_mat(spmatrix.ll_mat):
pass
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误
TypeError: Error when calling the metaclass bases
cannot create 'builtin_function_or_method' instances
Run Code Online (Sandbox Code Playgroud)
这是什么导致了这个错误?有没有办法使用委托,以便我的新类的行为与原始类完全相同?
在 C++ 中表示稀疏张量的合适数据结构是什么?我想到的第一个选项是 a,boost::unordered_map因为它允许快速设置和检索 an 元素等操作,如下所示:
A(i,j,k,l) = 5
Run Code Online (Sandbox Code Playgroud)
但是,我还希望能够对单个索引进行收缩,这将涉及对其中一个索引进行求和
C(i,j,k,m) = A(i,j,k,l)*B(l,m)
Run Code Online (Sandbox Code Playgroud)
使用 来实现这个运算符有多容易boost::unordered_map?有没有更合适的数据结构?
c++ ×6
python ×3
boost ×2
c++11 ×1
hashset ×1
html-lists ×1
indexing ×1
inheritance ×1
memory ×1
multi-index ×1
scipy ×1
shared-ptr ×1
sparse-array ×1
stl ×1
tie ×1
tuples ×1
typelib ×1
ublas ×1
vala ×1
valgrind ×1