我现在刚刚开始使用 Python 并学习类。我想知道是否可以将一个类作为参数传递给另一个类。我创建了 2 个类 - Point 类和 Circle 类。我希望 Circle 采用一个以坐标 x 和 y 以及半径作为参数的点。
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
return
# Takes a Point with coordonates x,y as center, and a radius
class Circle:
def __init__(self, Point, radius):
self.Point = Point
self.radius = radius
Run Code Online (Sandbox Code Playgroud) 我做了一个简单的C++程序.我期望浮点输出为5/9,但是为零.有人可以评论,为什么输出是意外的,即零?
#include<iostream>
using namespace std;
void fun(double *ptr);
int main (int argc, char* argv[])
{
double a;
fun(&a);
cout<<a<<endl; // why not floating point 5/9??
}
void fun (double *ptr)
{
*ptr=(5/9);
}
Run Code Online (Sandbox Code Playgroud) 如果我想传递cin作为参数函数,函数定义如何?例如:
ofstream plik("a.txt");
fun(cin, plik);
Run Code Online (Sandbox Code Playgroud)
那么有趣的定义是什么?
def fixData(lib):
for bookData in lib:
for dataElement in bookData:
if dataelement.endswith("\n"):
dataElement.rstrip("\n")
return lib
Run Code Online (Sandbox Code Playgroud) 我在hackerrank尝试代码但是没有工作.
#include<iostream>
using namespace std;
int add(int b, int c)
{
return b+c;
}
int main()
{
int a,b,c,i,sum;
cin>>a;
for(i=1;i<=a;i++)
{
std::cin>>b>>" ">>c;
sum=add(b,c);
cout<<sum<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
error : !cannot bind 'std::basic_istream::__istream_type {aka std::basic_istream}' lvalue to 'std::basic_istream&&'
Run Code Online (Sandbox Code Playgroud) 我有一些声明
vector<double> ved1(10), ved2(10), ved3(10);
array<double, 10> vod1, vod2, vod3;
valarray<double> vad1(10), vad2(10), vad3(10);
Run Code Online (Sandbox Code Playgroud)
如何从此版本转换代码
vad3 = 10.0* ((vad1 + vad2) / 2.0 + vad1 * cos(vad2));
Run Code Online (Sandbox Code Playgroud)
对此
transform(ved1.begin(), ved1.end(), ved2.begin(), ved3.begin(), plus<double>());
Run Code Online (Sandbox Code Playgroud)
仅使用vector-STL版本.
给定一组整数:
set<int> setA = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)
现在我想在一定条件下将整数插入整数向量:
vector<int> vectorB;
for (set<int>::iterator it = setA.begin(); it != setB.end(); it++){
if (*it % 2 == 0){
}else{
vectorB.insert((*it));
count += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误:
error: no matching function for call to 'std::vector<int>::insert(const int&)'
Run Code Online (Sandbox Code Playgroud)
为什么?