我试图找到一种更快的方法来将对添加到地图的末尾.目前,我在地图的末尾添加了对,因为我使用的键是for循环的索引,默认排序.所以我有:
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int> > temp;
for(int i = 0; i < 1000000; i++){
int first[] = {5,10,15,20,25};
int second[] = {10,20,30,40,50};
std::set<int> temp2;
temp2.insert(first, first + 5);
std::set<int> temp3;
temp3.insert(second, second + 5);
std::set<int> temp4;
std::set_union(temp2.begin(), temp2.end(), temp3.begin(), temp3.end(), std::inserter(temp4, temp4.end()));
temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4));
}
}
Run Code Online (Sandbox Code Playgroud)
当我计时时,大约需要10秒钟.但是,当我注释掉该行时temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4)),程序执行大约需要4秒钟.我想知道为什么将这对添加到地图需要花费很多时间.我是以最好的方式做到的吗?
我在寻求你对Perl的帮助.
我目前通过创建一个新对象然后使用它来运行一个函数来使我的代码运行良好,但我想使用更新的Perl编码方式(使用class:Std和集成BUILD/ START函数)
所以现在我的代码看起来像这样,我调用new一个日志文件的路径,然后运行一个函数:
# Called from another perl script
use MyClass;
my $Obj = MyClass->new('/usr/me/path.log');
$Obj->run();
# My module
package MyClass;
sub new {
shift if ( defined($_[0] eq 'MyClass') );
my %args = validate( @_,{ LogPath => { type => SCALAR, optional => 0 }, } );
my $self;
$self->{plistPath} = $_[1];
return bless $self;
}
sub run {
my $self = shift;
...
...
}
Run Code Online (Sandbox Code Playgroud)
而我想拥有的是这样的事情:
use Class::Std
sub BUILD …Run Code Online (Sandbox Code Playgroud) 我试图使用增加的向量transform,但不能正确地做.我想用变换.我究竟做错了什么?
PS - 我将使用c ++ 11标准和g ++.
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<double> x(10);
x.front() = 0.0;
double h = 0.1;
std::transform(x.begin(), x.end() - 1, x.begin() + 1, [h](unsigned int xn) {return xn + h;});
std::cout << x.at(3) << " " << x.at(9) << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我有一个结构msg:
struct msg {
//destination port
int addr;
//data
unsigned long long payload;
//prioritized flag
bool isPrio;
//construcor?
msg(int a, int p, bool b) : addr(a), payload(p),isPrio(b) { }
msg() : addr(0), payload(0), isPrio(false) { }
...
};
Run Code Online (Sandbox Code Playgroud)
还有一个通过SystemC distributor接收msgs sc_in并将一些元素推送到二维向量的类std::vector<std::vector <msg>> buffer:
class distributor: public sc_module {
public:
sc_vector<sc_in<msg>> inputMsg;
std::vector<std::vector <msg>> buffer;
int n, m, bufferSize;
...
distributor(sc_module_name name, int n, int m, int bufferSize) : //n -> number of …Run Code Online (Sandbox Code Playgroud) 我刚刚发现标准algorithm头中的几种算法不需要std::.
例:
#include <vector>
#include <algorithm>
int main() {
std::vector<int> m;
count(m.begin(), m.end(), 0);
count_if(m.begin(), m.end(), [](auto){return true;});
for_each(m.begin(), m.end(), [](auto){});
find_if(m.begin(), m.end(), [](auto){return true;});
}
Run Code Online (Sandbox Code Playgroud)
这有什么具体原因吗?双方g++并clang++接受上述代码.
默认函数来自std :: hash。我想知道是否有更好的哈希函数可以节省计算时间?用于整数键和字符串键。
我尝试使用Google的City Hash的整数和字符串键,但其性能比std :: hash差一些。
我最近有兴趣std::allocator,认为它可能解决我对C++代码的一些设计决定的问题.
现在我已经阅读了一些关于它的文档,观看了一些视频,比如Andrei Alexandrescu在2015年CppCon上的一个视频,我现在基本上明白我不应该使用它们,因为它们不是按照我认为分配器可能工作的方式工作的.
话虽如此,在实现这一点之前,我编写了一些测试代码,以了解自定义子类如何std::allocator工作.
显然,没有按预期工作...... :)
所以问题不在于如何在C++中使用分配器,而是我很想知道为什么我的测试代码(下面提供)不起作用.
不是因为我想使用自定义分配器.只是好奇看到确切的原因......
typedef std::basic_string< char, std::char_traits< char >, TestAllocator< char > > TestString;
int main( void )
{
TestString s1( "hello" );
TestString s2( s1 );
s1 += ", world";
std::vector< int, TestAllocator< int > > v;
v.push_back( 42 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完整的代码TestAllocator在本问题的最后提供.
在这里,我只是用我的自定义分配器一些std::basic_string,并用std::vector.
有了std::basic_string,我可以看到我的分配器的实例实际上是创建的,但是没有调用任何方法......
所以看起来它根本就没用过.
但是std::vector,我自己的allocate方法实际上被调用了.
那么为什么这里有区别?
我尝试过不同的编译器和C++版本.看起来像旧的GCC版本,使用C++ 98,会调用allocate我的TestString类型,但不会调用C++ 11及更高版本的新版本.铿也不打电话 …
为什么这段代码不能编译?
#include <map>
#include <list>
int main()
{
typedef std::list<int>::iterator Iter;
std::map<Iter, Iter> m;
std::list<int> ints;
m[ints.begin()] = ints.begin();
}
Run Code Online (Sandbox Code Playgroud)
如果我改变std::list了std::vector,一切正常.
错误日志如下:
|| "==== Building Practical (release) ===="
|| main.cpp
In file included from C:\mingw32\i686-w64-mingw32\include\c++\bits\stl_tree.h|65| 0,
|| from C:/mingw32/i686-w64-mingw32/include/c++/map:60,
|| from ../src/main.cpp:1:
|| C:/mingw32/i686-w64-mingw32/include/c++/bits/stl_function.h: In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::_List_iterator<int>]':
C:\mingw32\i686-w64-mingw32\include\c++\bits\stl_map.h|498 col 32| required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = …Run Code Online (Sandbox Code Playgroud) 在程序中,经常在不同的类中生成随机数。因此,我想创建一个返回生成器std :: mt19937的单个实例的类。我还考虑到某些编译器不能与std :: random_device一起使用(为此,请检查熵的值)。我创建了一个类单例。
#include <iostream>
#include <random>
#include <chrono>
class RandomGenerator
{
public:
static RandomGenerator& Instance() {
static RandomGenerator s;
return s;
}
std::mt19937 get();
private:
RandomGenerator();
~RandomGenerator() {}
RandomGenerator(RandomGenerator const&) = delete;
RandomGenerator& operator= (RandomGenerator const&) = delete;
std::mt19937 mt;
};
RandomGenerator::RandomGenerator() {
std::random_device rd;
if (rd.entropy() != 0) {
mt.seed(rd());
}
else {
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
mt.seed(seed);
}
}
std::mt19937 RandomGenerator::get() {
return mt;
}
int main() {
std::mt19937 &mt = RandomGenerator::Instance().get();
std::uniform_real_distribution<double> dist(0.0, 1.0); …Run Code Online (Sandbox Code Playgroud) 如果我们使用结构或类作为键,则需要比较函数将值放在树中,但如果将一对用作键,那么地图数据结构如何将值放在树中.即必须有一些东西来比较密钥并将它们存储在树中.