我有一个Rectangle
类转换运算符,double
并且std::string
:
class Rectangle
{
public:
Rectangle(double x, double y) : _x(x), _y(y) {}
operator std::string ();
operator double ();
private:
double _x, _y;
double getArea() {return _x * _y;}
};
int main()
{
Rectangle r(3, 2.5);
cout << r << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么operator double()
被调用,而不是operator std::string()
.据我所知,根据C++ wikibook,
operator double
用于将Rectangle
对象转换为double
.
那么这里发生了什么?是否与int
传递给构造函数的事实有关?如果是这样,为什么?
我写了一个简单的模块叫做Test
:
module Test (main) where
main =
putStrLn "Hello"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过以下命令行编译它时:
ghc Test.hs -o my-program
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
[1 of 1] Compiling Test ( Test.hs, Test.o )
<no location info>: error:
output was redirected with -o, but no output will be generated because there is no Main module.
Run Code Online (Sandbox Code Playgroud) #include <iostream>
struct A {
void init()
{
internal_init();
}
virtual void internal_init()
{
std::cout << "internal of A" << std::endl;
}
};
struct B: public A {
void internal_init()
{
init();
std::cout << "internal of B" << std::endl;
}
};
int main(){
B instance;
std::cout << "internal of A" << std::endl;
instance.internal_init();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
首先,该计划B::internal_init()
按预期进行.然后,到A::init()
(我猜因为B来自A,而B没有任何init()
).怎么办?
什么internal_init()
它会选择谁?因为它进入B::internal_init()
,程序将进入无限循环,我不明白为什么.
internal_init()
?internal_init()
实例的"B部分"?这是关于"虚拟"吗?如果是这样,怎么样?虚函数在我们使用多态时发生(就像我自己理解的初学者一样,它使用指向派生类对象的基类的指针).#include <iostream>
using namespace std;
int main() {
//int& a = 3; <- Doesn't compile. Expression must be lvalue.
const auto& c = 1 + 2; // c is a constant reference to an int. (?)
// compiles fine. 1+2 is a rvalue? what's going on?
cout << c << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么编译器不会引发编译错误.由于auto"强制"c是对常量int的引用,并且引用被引用到左值,为什么它会起作用?
我们假设有一个进程,PID = 1
它运行以下代码:
int a = fork();
int b = fork();
printf(“a: %d, b: %d\n”, a, b);
Run Code Online (Sandbox Code Playgroud)
让我们进一步假设,新的PID
旨意被赋予一个接一个,如此发出第二个PID
会2
,然后3
等
可能的输出是:
a:2, b:3
a:2, b:0
a:0, b:4
a:0, b:0
Run Code Online (Sandbox Code Playgroud)
我有一些麻烦试图理解上面代码的输出,尤其是为什么a:0, b:4
和a:2, b:3
.
#include <iostream>
int getID ( int k ) {
static int& r = k;
return r++;
}
int main()
{
int a = getID ( 10 );
int b = getID ( 10 );
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这个代码编译.
考虑下一段代码.
#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<pthread_t,vector<int>> map_vec;
vector<pair<pthread_t ,int>> how_much_and_where;
pthread_cond_t CV = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* writer(void* args)
{
while(*some condition*)
{
int howMuchPush = (rand() % 5) + 1;
for (int i = 0; i < howMuchPush; ++i)
{
// WRITE
map_vec[pthread_self()].push_back(rand() % 10);
}
how_much_and_where.push_back(make_pair(pthread_self(), howMuchPush));
// Wake up the reader - there's something to read.
pthread_cond_signal(&CV);
}
cout << "writer thread: " << pthread_self() << endl;
return nullptr;
} …
Run Code Online (Sandbox Code Playgroud) 我想有效地迭代给定图像的每个像素,并根据像素的位置,应用一个函数并将输出放在这个位置。
这是我尝试过的:
def point_gaussian_value(p1, p2, sigma=0.8):
x1, y1 = p1
x2, y2 = p2
return np.exp(-1 * (np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) / sigma ** 2))
point_annotation = np.zeros_like(sample).astype(np.float32)
for j in range(sample.shape[0]):
for k in range(sample.shape[1]):
value = point_gaussian_value(p1=(j, k), p2=(row[header[i]], row[header[i + 1]]))
point_annotation[j, k] = point_gaussian_value(p1=(j, k), p2=(20, 20))
Run Code Online (Sandbox Code Playgroud)
尽管它很幼稚,而且效率极低,特别是对于大图像。我知道我可以矢量化一个函数,这样所有计算都将同时有效地完成,但我不确定它是否可能/如何矢量化一个函数,以获得每个像素的像素位置。
#include <iostream>
class A{};
class AA:public A{};
struct X {
void f(A *) {
std::cout << "X::f(A*)\n";
}
};
struct Y:public X {
void f(A *) {
std::cout << "Y::f(A*)\n";
}
};
int main() {
Y *y = new Y();
X *x = new Y();
const X *cx = new Y();
y->X::f(new AA);
x->f(new AA);
}
Run Code Online (Sandbox Code Playgroud)
打印:
X :: F(A*)
X :: F(A*)
我不明白为什么y->X::f(new AA)
,x->f(new AA)
不会引起编译错误.X::f(A *)
在两种情况下我都会调用它.但编译器选择使用此方法的原则是什么?函数重载多态?任何拇指规则?
假设我定义了一张全球地图 -
map<int,list<char>> cMap;
Run Code Online (Sandbox Code Playgroud)
有没有办法(不使用boost
库)我可以添加整数键,稍后在程序中添加值到与它们对应的列表?
map<int,list<char>> cmap;
int main()
{
// Add only a value this way?
cmap[2];
// and then -
cmap[2].push_back('A');
return 0;
}
Run Code Online (Sandbox Code Playgroud) <EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColorHint="#E6E6E6"
android:inputType="textPassword"
android:hint="?????"
android:id="@+id/etLoginPassword"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:singleLine="true"
android:imeOptions="actionDone"/>
Run Code Online (Sandbox Code Playgroud)
当我改变时android:inputType
,TextHint
它“扔”到左边。当我将重力更改为“正确”时,它会返回到它需要的位置(希伯来语)。但是,我希望密码(用 EditText 编写的文本)重力为“左”(密码应该用英文字符书写)。我怎样才能克服这个问题?
考虑下一段代码:
data Tile = EmptyTile | X | O
data Player = Player1 | Player2
instance Show Tile where
show EmptyTile = " "
show X = "X"
show O = "O"
data Board = (Tile, Tile, Tile, Tile, Tile, Tile, Tile, Tile, Tile)
emptyBoard :: Board
emptyBoard = (EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile,EmptyTile)
instance Monad Board where
return x = x
f >>= x = x
Run Code Online (Sandbox Code Playgroud)
我希望那个董事会成为一个单子.但问题是我得到以下错误 -
[1 of 1] Compiling Main ( Main.hs, Main.o )
Main.hs:17:14: error:
Illegal binding of built-in …
Run Code Online (Sandbox Code Playgroud)