我正在使用c ++ STL堆算法,我在它周围写了一个包装类,所以我可以做一些其他的东西.当我尝试使用下面的代码时,例如:
//! Min-heap wrapper class.
class FMMHeap{
public:
FMMHeap(Vector &phi) : _phi(phi) {}
bool operator()(unsigned p1, unsigned p2) {return fabs(_phi(p1)) > fabs(_phi(p2)); }
inline void pop(){ pop_heap(_heap.begin(),_heap.end(),*this); _heap.pop_back(); }
[...lots of other stuff...]
vectorU32 _heap;
Vector &_phi;
}
Run Code Online (Sandbox Code Playgroud)
它比我有一个像这样的单独的函数对象慢得多:
struct HeapSort{
public:
HeapSort(Vector &phi) : _phi(phi) {}
bool operator()(unsigned p1, unsigned p2) {return fabs(_phi(p1)) > fabs(_phi(p2)); }
private:
Vector &_phi;
};
class FMMHeap{
public:
FMMHeap(Vector &phi) : cmp(phi) {}
inline void pop(){ pop_heap(_heap.begin(),_heap.end(),cmp); _heap.pop_back(); }
[...lots of other …Run Code Online (Sandbox Code Playgroud) 作为练习我正在制作模板Array类,我想执行此操作:
Array<int> a[5];
a[4] = 2;
Run Code Online (Sandbox Code Playgroud)
我怎么写这样的东西?
我试过了:
template<class T> class Array{
...
T operator[(const int loc)]=(const T temp);
Run Code Online (Sandbox Code Playgroud) 这是我用C++编写的源文件.
#include<string>
#include<sstream>
#include "Lecture.hpp"
#include <iostream>
using namespace std;
Lecture::Lecture() {
capacity=5;
log = new int[capacity];
used = 0;
}
/*
* The following is copy constructor.
*/
Lecture::Lecture(Lecture& orig) {
copy(&orig);
}
/*
* This is an empty destructor
*/
Lecture::~Lecture() {
// dereference dynamic memory
}
Lecture & Lecture:: operator=(Lecture & other){
this->copy(&other);
return *this;
}
/*
* Copy method.
*/
void Lecture::copy(Lecture &other){
if(&other != this){
capacity = other.capacity;
log = new int[capacity];
used = other.used; …Run Code Online (Sandbox Code Playgroud) 我有
public abstract class DataClass
{
public static bool operator ==(DataClass left, DataClass right)
{
return left.Equals(right);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是发生的事情
object left = new DataClass();
object right = new DataClass();
bool expected = true;
bool actual;
actual = ((DataClass)left) == ((DataClass)right);
Assert.AreEqual(expected, actual); // passes
actual = left == right;
Assert.AreEqual(expected, actual); // fails
Run Code Online (Sandbox Code Playgroud)
如何让它调用正确的实现,而不是显式地强制它?
std :: array即将获得
no match for ‘operator=’ in ‘myarr = {1, 5, 2, 3, 4}’
Run Code Online (Sandbox Code Playgroud)
编译此代码时出错
#include <iostream>
#include <array>
using namespace std;
int main(int argc, char const *argv[])
{
array<int, 5> myarr;
myarr = {1,5,2,3,4};
for(auto i : myarr)
{
cout << i << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我在同一条线上进行编译时它会编译
array<int, 5> myarr = {1,5,2,3,4};
Run Code Online (Sandbox Code Playgroud)
如何在分隔线上分配值
我需要在类构造函数中赋值,我该怎么办?
class myclass
{
myclass()
{
myarr = {1,2,3,4,5}; /// how to assign it // it gives errors
}
};
Run Code Online (Sandbox Code Playgroud) I tried to overload the operator !=
struct Antichoc
{
quint8 Chariot;
quint8 Frittage;
bool operator!=(const Antichoc &a, const Antichoc &b)
{
return a.Chariot != b.Chariot || a.Frittage != b.Frittage;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
bool Antichoc :: operator!=(const Antichoc&,const Antichoc&)必须只有一个参数
为什么这个错误
似乎golang没有C和C ++拥有的指针运算符->。现在让我们说我有一个类似如下的函数:myfun(myparam * MyType),在函数内部,如果要访问MyType的成员变量,则必须执行(* myparam).MyMemberVariable。在C和C ++中执行myparam-> MyMemberVariable似乎容易得多。
我很新。不确定我是否缺少某些东西,否则这不是正确的方法吗?
谢谢。
当我运行这个脚本时:
#!/bin/bash
if [[ "abcd" =~ ^.*$ ]]; then
echo "something"
fi
Run Code Online (Sandbox Code Playgroud)
我明白了:
./tmp2.sh: line 3: conditional binary operator expected
./tmp2.sh: line 3: syntax error near `=~'
./tmp2.sh: line 3: `if [[ "abcd" =~ ^.*$ ]]; then'
Run Code Online (Sandbox Code Playgroud)
我已经尝试了我找到的所有建议,但仍然是一样的:/请帮助我!
我有以下列表:
languages =["java", "haskell", "Go", "Python"]
animals = ["pigeon", "python", "shark"]
names = ["Johan","Frank", "Sarah"]
Run Code Online (Sandbox Code Playgroud)
我想知道以下所有三个列表中是否存在python.以下if语句是我使用"in"方法和"and"运算符得出的结果.
if("Python" in languages and "Python" in animals and "Python" in names )
Run Code Online (Sandbox Code Playgroud)
有没有办法将这个语句压缩成较小的长度?
IE
if("Python" in languages and in animals and in names)
Run Code Online (Sandbox Code Playgroud) 虽然我不知道你不能operator[]在C++中重载以接受多个参数,但我偶然发现了一个似乎对我有意义的声明:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> numbers{1, 2, 3, 4};
int i = 0;
std::cout << numbers[i++,i+=1,i=1,i+1] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么有人可以解释是否有任何传递多个表达式的好处operator[]?
用mingw g ++ 4.8.1编译,带-std = c ++ 11
operator-keyword ×10
c++ ×6
.net ×1
arrays ×1
bash ×1
c# ×1
c++11 ×1
class ×1
copy ×1
go ×1
inheritance ×1
list ×1
methods ×1
operators ×1
overloading ×1
overriding ×1
performance ×1
pointers ×1
python ×1
regex ×1
shell ×1
std ×1
templates ×1
this ×1