#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int square(int a){
return a*a;
}
struct Point{
int x,y;
};
int distance (const Point& a,const Point& b){
int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
return k;
}
int main(){
vector<Point>a(10);
for (int i=0;i<10;i++){
cin>>a[i].x>>a[i].y;
}
int s=0;
int s1;
int k=0;
for (int i=1;i<10;i++){
s+=square(distance(a[0],a[i]));
}
for (int i=1;i<10;i++){
s1=0;
for (int j=0;j<10;j++){
s1+=square(distance(a[i],a[j]));
if (s1<s) { s=s1; k=i;}
}
}
cout<<k<<"Points are:";
cout<<a[k].x;
cout<<a[k].y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有以下代码,但这里是错误列表
1>------ Build started: Project: distance, …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors using-directives overload-resolution name-lookup
我已经读过一个地方,for当你迭代某种数组/序列/列表/你调用它时,你应该使用一个循环,当你的循环需要在某个条件下停止时,我应该使用while循环.
那么,如果我有这样的东西怎么办?
int len = 0;
for(; s[len] != '\n'; ++len) {
// some processing
if (someCondition) {
return len;
}
}
// if done iterating, return len
return len;
Run Code Online (Sandbox Code Playgroud)
for在这种情况下可以使用循环还是while循环更好?
我正在尝试建立一个数字介于1和插入数字之间的金字塔.例如,如果我将6插入整数,则piramid将如下所示:
12345654321
234565432
3456543
45654
565
6
Run Code Online (Sandbox Code Playgroud)
我尝试使用for循环,但我得到任何一行或++数字到6.这是代码:
#include<stdio.h>
#include <iostream>
#include <conio.h>
int main()
{
int i,j,d;
std::cin >> d;
for(i=1;i<=d;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题,如图所示构建金字塔.
我坚持这个问题2天了.有人可以帮我逻辑吗?
我正在研究C++程序以获得良好的算法.我现在正在研究Danielson-Lanczos算法来计算序列的FFT.
看着
mmax=2;
while (n>mmax) {
istep = mmax<<1;
theta = -(2*M_PI/mmax);
wtemp = sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for (m=1; m < mmax; m += 2) {
for (i=m; i <= n; i += istep) {
j=i+mmax;
tempr = wr*data[j-1] - wi*data[j];
tempi = wr * data[j] + wi*data[j-1];
data[j-1] = data[i-1] - tempr;
data[j] = data[i] - tempi;
data[i-1] += tempr;
data[i] += tempi;
}
wtemp=wr;
wr += wr*wpr …Run Code Online (Sandbox Code Playgroud) 我想检查两个向量是否有任何共同的元素.这个语法出了什么问题?
// Check whether the current list and the input l2 share any nodes or not
bool shared(const VectorList< NODETYPE > &l2);
template< typename NODETYPE > //SHARED
bool VectorList< NODETYPE>::shared(const VectorList< NODETYPE > &l2)
{
for(int i = 0; i < (int)vList.size(); i++)
{
for (int j = i; j < (int)l2.size() ; j++)
{
if (vList[i] == l2[j])
{
return(1);
}
}
}
return(0);
}
Run Code Online (Sandbox Code Playgroud) 我陷入了简单的矢量输入和输出操作.编译器返回错误'std :: outof range'
这是代码
int main()
{
int size;
cout <<"Enter size of vector\n";
cin>>size;
cout<<"Now to input the vector of size "<<size<<endl;
vector <int> trial;
for (size_t i=0;i<size;++i){
int x;
cout<<"write at position"<<trial.at(i)<<'t';
cin>>x;
trial.push_back(x);
cout<<endl;
}
ostream_iterator<int> output(cout,"");
copy(trial.begin(),trial.end(),output);
}
Run Code Online (Sandbox Code Playgroud)
我将很感激对该问题内部运作的简要解释.
我正在尝试为我的结构编写一个复制构造函数,但我似乎没有做到这一点,并希望得到任何可能的帮助.我想以递归的方式做一个深层复制,但是我一直在初始化初始化列表中得到建议,这似乎也没有好转.
struct Node
{
Node* left; // will be our previous
Node* right;// will be our next
Node* previous;// get a handle to the previous node
string value;
Node(string nval): left(NULL), right(NULL), previous(NULL), value(nval)
{
}
Node(Node const& node)
: previous(new Node(node.previous)),
left(new Node(node.left)),
right(new Node(node.right)),
value(node.value)
{
}
Node& operator=(const Node&)
{
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在使用这里的以下示例
考虑我有以下课程
#include <iostream>
class Distance {
private:
int feet;
int inches;
public:
Distance() : feet(), inches() {}
Distance(int f, int i) : feet(f), inches(i) {}
friend std::ostream &operator<<( std::ostream &output, const Distance &D )
{
output << "F : " << D.feet << " I : " << D.inches;
return output;
}
friend std::istream &operator>>( std::istream &input, Distance &D )
{
input >> D.feet >> D.inches;
return input;
}
};
Run Code Online (Sandbox Code Playgroud)
我正在使用 Gtest 来测试这个类。
但我找不到更好的方法来测试它。
我可以使用 gtest 中提供的宏ASSERT_NO_THROW …
我有一个问题,让我对我必须做的事感到困惑.问题如下:
Modify_If的原型如下:
template <class C, class Tester, class Transform>
void Modify_If(C & a, Tester f, Transform g)
Run Code Online (Sandbox Code Playgroud)
我必须编写Modify_If函数,但我不知道从哪里开始,所以如果有人可以帮助我,我会很感激.
初始化从其基类中转换的派生类的首选方法是什么?
请考虑以下情形:
class A{
public:
A();
~A();
}
class B : public A{
public:
B() {m_b = 0.0;};
~B();
float GetValue(){return m_b;};
private:
float m_b;
}
A* a = new A;
B* b = static_cast<B*>(a);
float val = b->GetValue(); // This was never initialized because it was not constructed
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案是手动调用Initialize()函数,该函数将像构造函数那样执行必要的初始化.
虽然看起来很草率,但必须有一个更好/更清洁的方法.
非常感谢任何帮助和指导!
c++ ×10
stl ×2
vector ×2
algorithm ×1
c ×1
cin ×1
constructor ×1
dft ×1
googletest ×1
initializing ×1
name-lookup ×1
visual-c++ ×1