我正在尝试对concurrent_vector类型进行排序,其中hits_object:
struct hits_object{
unsigned long int hash;
int position;
};
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
concurrent_vector<hits_object*> hits;
for(i=0;...){
hits_object *obj=(hits_object*)malloc(sizeof(hits_object));
obj->position=i;
obj->hash=_prevHash[tid];
hits[i]=obj;
}
Run Code Online (Sandbox Code Playgroud)
现在我已经填满了一个concurrent_vector<hits_object*>电话hits.
但我想在position属性上对这个concurrent_vector进行排序!
以下是典型命中对象内部的示例:
0 1106579628979812621
4237 1978650773053442200
512 3993899825106178560
4749 739461489314544830
1024 1629056397321528633
5261 593672691728388007
1536 5320457688954994196
5773 9017584181485751685
2048 4321435111178287982
6285 7119721556722067586
2560 7464213275487369093
6797 5363778283295017380
3072 255404511111217936
7309 5944699400741478979
3584 1069999863423687408
7821 3050974832468442286
4096 5230358938835592022
8333 5235649807131532071
Run Code Online (Sandbox Code Playgroud)
我想根据第一列(类型的"位置" int)对此进行排序.第二列是类型的"哈希" unsigned long int.
现在我尝试做以下事情:
std::sort(hits.begin(),hits.end(),compareByPosition);
其中compareByPosition定义为:
int compareByPosition(const void …Run Code Online (Sandbox Code Playgroud) 此代码按预期工作(在线处).最后v是空的,w因为它已经窃取了内容,所以不是空的v.
vector<int> v;
v.push_back(1);
cout << "v.size(): " << v.size() << endl;
auto vp = move(v);
vector<int> w(vp);
cout << "w.size(): " << w.size() << endl;
cout << "v.size(): " << v.size() << endl;
Run Code Online (Sandbox Code Playgroud)
但是,如果我取代auto vp=move(v)用
vector<int> && vp = move (v);
Run Code Online (Sandbox Code Playgroud)
然后它不动.相反,它复制,两个向量最后都是非空的.如图所示这里.
澄清:更具体地说,什么是自动衍生类型vp?如果不是vector<int> &&,那还有什么呢?为什么这两个例子虽然如此相似但却给出了不同的结果?
额外:我也试过这个,它仍然复制而不是移动
std :: remove_reference< vector<int> > :: type && vp = move(v);
Run Code Online (Sandbox Code Playgroud) 如你所知,有一些方法可以解决河内塔,但是他们要求所有磁盘在开始时都在一个塔中.
现在我想知道有没有办法解决它,其中磁盘已经开始在塔之间随机分布.
我有一个类A,它是类B和C的父类.还有一个类X,它是Y和Z的父类.
class A {};
class B : public A {};
class C : public A {};
class X
{
void foo(A) { std:: cout << "A"; }
};
class Y : public X
{
void foo(B) {std::cout << "B"; }
};
class Z : public X
{
void foo(c) {std<<cout <<"C"; }
};
int main()
{
B b;
C c;
Y y;
Z z;
y.foo(b);//prints B // b is a B, and Y::foo takes a B, hence print B
y.foo(c);//prints …Run Code Online (Sandbox Code Playgroud) 我想知道为什么std::function只知道两个参数的功能.我写了一些运行良好的代码,但是有很多限制.任何反馈欢迎.特别是,我怀疑我正在重新发明轮子.
我的代码是关于ideone的,我将参考它.
例如,我可以用以下方式描述main:
function_type_deducer(main).describe_me();
// Output: I return i and I take 2 arguments. They are of type: i PPc
Run Code Online (Sandbox Code Playgroud)
(其中'i'表示'int','PPc'表示指向指针指向char的指针)
Standard std::function不适用于具有两个以上args的函数(请参阅我的代码的最后两行),但此代码可以使用(示例代码演示了三个arg函数).也许我的设计应该在标准库中使用!我定义typedef tuple<Args...> args_as_tuple;存储所有args,而不仅仅是前两个参数类型.
主要技巧是这个函数的推论:
template<class T, class... Args>
auto function_type_deducer(T(Args...)) -> Function__<T, Args...> {
return Function__<T, Args...> {};
}
Run Code Online (Sandbox Code Playgroud)
限制:
function_type_deducer([](){}).describe_me();x和y,如y需要string&,哪里x需要string.(std :: function也没有注意到这一点)关于如何修复其中任何一个的任何想法?我重新改造了车轮吗?
例如:
int input;
cout << "Please enter how many burgers you'd like" << endl;
cin >> input;
Run Code Online (Sandbox Code Playgroud)
什么是缩短“输入”和只接受前两个最简单的方法位数字。继续这个例子:
用户输入:432534。输入值:43。
用户输入:12342342341234123412341235450596459863045896045。输入值:12。
(编辑:说“数字”而不是“位”)
比方说我有:
vector<T *> vect;
Run Code Online (Sandbox Code Playgroud)
我知道这不是明显的方法,但我有一个API可以做到这一点,所以我不能决定使用它vector<T>.
所以当我把对象放进去的时候我会:
vect.push_back(new T);
Run Code Online (Sandbox Code Playgroud)
这段代码是否会确保此向量是连续对齐的,因为它是指针的向量?这个容器会决定new操作员的行为吗?
编辑:我还需要打电话给delete那些吗?
你有以下几点:
Person& getPersonByName(string name);
Run Code Online (Sandbox Code Playgroud)
在什么情况下你需要担心一旦方法结束就会破坏getPersonByName中的返回者,这样调用者方法就可以处理被破坏的数据?
谢谢
当我想知道数组的大小时,我会执行以下操作:
int array[30];
for(int i = 0; i < 30; i++)
array[i] = i+1; //Fill list
const int size = sizeof(array) / sizeof(array[0]);
Run Code Online (Sandbox Code Playgroud)
但是当我在函数中将数组作为参数传递时,我将在函数中有一个指针.
int size( int array[] )
{
return sizeof(array) / sizeof(array[0]); //Doesn't work anymore
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用.但是如何在不使用大小的另一个参数的情况下获取函数中该数组的大小?
我想我真的在问:别名是"传递性的"吗?如果编译器知道A可能是别名B,而B可能是别名C,那么肯定它应该记住A可能因此别名C.也许这个"明显的"传递逻辑不是必需的吗?
一个例子,为了清楚起见.对我来说,最有趣的例子是严格别名问题:
// g++ -fstrict-aliasing -std=c++11 -O2
#include <iostream>
union
{
int i;
short s;
} u;
int * i = &u.i;
int main()
{
u.i = 1; // line 1
*i += 1; // line 2
short & s = u.s;
s += 100; // line 3
std::cout
<< " *i\t" << *i << std::endl // prints 2
<< "u.i\t" << u.i << std::endl // prints 101
;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
克++ 5.3.0,在x86_64(但不铛3.5.0)给出上面的输出,其中*i和u.i给予不同的号码.但是它们应该给出完全相同的数字,因为它i被定义为 …
我有以下宏:
#define ASSERT_ITERATOR_VALUE_TYPE(Iterator__, Value_type__) \
static_assert(std::is_same<Value_type__, typename Iterator__::value_type>::value, \
"Expected iterator with value type #Value_type__")
Run Code Online (Sandbox Code Playgroud)
在上面的宏中,我试图Value_type__在作为第二个输入参数的字符串文字中插入/附加标记static_assert.
显然,这不是我想要实现的,因为如果我将宏声明为:
ASSERT_ITERATOR_VALUE_TYPE(std::set<int>::iterator, double);
Run Code Online (Sandbox Code Playgroud)
我会收到消息:
error: static assertion failed: Expected iterator with value type #Value_type__
^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
相反,我想接受这个消息:
error: static assertion failed: Expected iterator with value type double
^^^^^^
Run Code Online (Sandbox Code Playgroud)
是否有某种预处理器巫术可以帮助我达到我想要的目标?
为什么要d.f(1)调用Derived::f此代码?
是否using Base::f决定其发挥作用f将被调用?
#include <iostream>
using namespace std;
struct Base {
void f(int){
cout << "\n f(Base) is called" ;
}
};
struct Derived : Base {
using Base::f ; // using-declarations but still Drived function is called
void f(int){
cout << "\n f(Derived) is called" ;
}
};
void use(Derived d)
{
d.f(1); // calls Derived::f
Base& br = d ;
br.f(1); // calls Base::f
}
int main() {
Derived d; …Run Code Online (Sandbox Code Playgroud) 我在使用时遇到了一些问题std::list.我可以很好地使用它与"简单"类型的数据列表,或一种类型的对象列表.
我想创建一个包含三种不同类型对象的列表.例如,有三种类型的对象:类p1,类p2,类p3.我想创建一个包含这三个对象的列表.
class p1 {
...
};
class p2 {
...
};
class p3 {
...
};
int main() {
std::list<whatshouldiInserthere?> namelist;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过模板,但没办法.你能发布一个示例源代码吗?