Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用结构定义函数 我是一个新手,特别是C++。我有一个任务,它的一部分是使用结构编写函数 struct S { float m; //how many int h; //where float mx; }; int main() { S s; s.m=0.5; s.h=1; vector<float> v(10); for (int i=0;i<10;i++) v[i]=sin(i); S mx = max_search(v); 结构{ float m;//有多少 int h;//其中 浮动mx; }; int main(){ S S; s、 m=0.5; s、 h=1; 向量v(10); for(int i=0;i0.98935&&mx.m_C++_Function_Vector_Struct - Fatal编程技术网

使用结构定义函数 我是一个新手,特别是C++。我有一个任务,它的一部分是使用结构编写函数 struct S { float m; //how many int h; //where float mx; }; int main() { S s; s.m=0.5; s.h=1; vector<float> v(10); for (int i=0;i<10;i++) v[i]=sin(i); S mx = max_search(v); 结构{ float m;//有多少 int h;//其中 浮动mx; }; int main(){ S S; s、 m=0.5; s、 h=1; 向量v(10); for(int i=0;i0.98935&&mx.m

使用结构定义函数 我是一个新手,特别是C++。我有一个任务,它的一部分是使用结构编写函数 struct S { float m; //how many int h; //where float mx; }; int main() { S s; s.m=0.5; s.h=1; vector<float> v(10); for (int i=0;i<10;i++) v[i]=sin(i); S mx = max_search(v); 结构{ float m;//有多少 int h;//其中 浮动mx; }; int main(){ S S; s、 m=0.5; s、 h=1; 向量v(10); for(int i=0;i0.98935&&mx.m,c++,function,vector,struct,C++,Function,Vector,Struct,您希望您的返回最外层的max;。现在它返回for循环的每个迭代,这意味着您只得到一个迭代 float max_search(vector<float> v) { float max=0.0f; <------------ for (int i=0; i<v.size(); i++) { if (v[i]>max) { max=v[i]; } -------------- }

您希望您的
返回最外层的max;
。现在它返回for循环的每个迭代,这意味着您只得到一个迭代

float max_search(vector<float> v) {
    float max=0.0f;    <------------
    for (int i=0; i<v.size(); i++) {
       if (v[i]>max) {
        max=v[i];
       }
    -------------- 
    }
    return max;   <------------
}

如果将函数声明为
float
,为什么返回
int

float max_search(vector<float> v) {
  float max = v[0]; //this way you avoid an iteration
  for (int i = 1; i < v.size() - 1; i++)
    if (v[i] > max) max = v[i];
  return max;
}
float max_搜索(向量v){
float max=v[0];//这样可以避免迭代
对于(int i=1;imax)max=v[i];
返回最大值;
}
您还可以使用迭代器来执行此操作:

float max_search(vector<float> v) {
  float max = .0;
  for (vector<float>::iterator it = v.begin(); it != v.end(); ++it)
    if (*it > max) max = *it;
  return max;
}
float max_搜索(向量v){
浮动最大值=.0;
for(vector::iterator it=v.begin();it!=v.end();++it)
如果(*it>max)max=*it;
返回最大值;
}
在第一个代码块中,重要的是将1减去
v.size
,否则您将尝试访问不存在的元素。如果代码没有返回分段错误,那是因为
std::vector
是访问安全的。这意味着
std::vector尝试访问该元素,但无论如何,您正在进行最后一次必要的迭代。这就是为什么使用迭代器更好的原因

@KarthikT所说的也是正确的:您试图在每次迭代中返回
max
,因此,在第一次迭代后,函数返回值并停止执行,始终检索向量的第一个值(如果该值大于0)


我希望这有帮助。

不确定我是否正确捕获了你的主要问题。你想将max_search函数的返回值(即
浮点)转换为struct S
?我将介绍KarithikT的答案并添加更多详细信息:

要启用
隐式转换
(从浮点到结构),需要将转换函数添加到
S

struct S {
  S():m(0.0), h(0), mx(0.0){ }         //
  S(float x):m(0.0), h(0), mx(x){  }   // to enalbe convert float to S
    float m; //how many
    int h; //where
    float mx;    
};

float max_search(const vector<float>& v) { // pass by const reference
    float max=0.0f;  
    for (int i=0; i<v.size(); i++) {
       if (v[i]>max) {
        max=v[i];
       }
    }
    return max;  
}
结构{
S():m(0.0),h(0),mx(0.0){//
S(float x):m(0.0),h(0),mx(x){}//启用将float转换为S
float m;//有多少
int h;//其中
浮动mx;
};
float max_搜索(常量向量&v){//通过常量引用传递
浮动最大值=0.0f;
对于(int i=0;imax){
max=v[i];
}
}
返回最大值;
}
您还可以使用std::max_元素从容器中查找max元素:

vector<float> v(10);
for (int i=0;i<10;i++) {
   v[i]=sin(i);
 }
S mx = *std::max_element(v.begin(), v.end());
向量v(10);
for(int i=0;iYou正在for循环中返回。Soit将只执行一次。由于不需要复制向量,请通过
const
reference传递它。在第二个示例中,您既没有声明(或初始化)
max
也没有返回它…再次编辑。在第一个块代码中,我更改了
max=v[1]
对于
max=v[0]
.Aww,谢谢我不在意,我纠正了max\u search()中的错误。事实上,你是对的,但我不能在main()中更改任何内容,这就是为什么
s mx=max\u search(v)
必须保留的原因。所以我一直收到这样的消息:“从‘float’转换为非语常会类型‘s’。”当然,因为你真正想做的是
s.mx=max_search(v);
@user1846813如果你不能更改的代码是由你的导师给出的,你可能需要再次确认它是否是一个拼写错误,因为整个语句都是拼写错误!
struct S {
  S():m(0.0), h(0), mx(0.0){ }         //
  S(float x):m(0.0), h(0), mx(x){  }   // to enalbe convert float to S
    float m; //how many
    int h; //where
    float mx;    
};

float max_search(const vector<float>& v) { // pass by const reference
    float max=0.0f;  
    for (int i=0; i<v.size(); i++) {
       if (v[i]>max) {
        max=v[i];
       }
    }
    return max;  
}
vector<float> v(10);
for (int i=0;i<10;i++) {
   v[i]=sin(i);
 }
S mx = *std::max_element(v.begin(), v.end());