質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1597閲覧

反復子itの使い方がわかりません。

langhtorn

総合スコア104

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/07/03 17:41

###実現したいこと
最小包囲長方形を求めたいです。
Rectangle bouding_rectangle関数の中で以下のようなエラーが出る理由がわかりません。
また、この関数内で反復子itを長方形のメンバとしたいそのやり方がわかりません。
###試したこと

この関数内で反復子itを長方形のメンバとしたいそのやり方がわかりません。

いくつか考えてみました。
(*it).lx=std::min...
it.lx=std::min...
どれもだめでした。
###エラー

error: declaration of 'auto it' has no initializer 47 | auto it; | ^~~~

###コード

C++

1//点集合の包囲長方形 2 3#include<iostream> 4#include<vector> 5#include<algorithm> 6 7//座標を表現するクラス 8struct Point 9{ 10 double x; //x座標 11 double y; //y座標 12}; 13 14//長方形を表現するクラス 15struct Rectangle 16{ 17 double lx; //左辺のx座標 18 double rx; //右辺のx座標 19 double dy; //下辺のy座標 20 double uy; //上辺のy座標 21}; 22 23//Rectangleに対する出力用の演算子 24std::ostream& operator<<(std::ostream& out,Rectangle r) 25{ 26 out<<"左辺のx座標"<<r.lx<<" 右辺のx座標"<<r.rx<<"下辺のx座標"<<r.dy<<"上辺のx座標"<<r.uy; 27 return out; 28} 29 30//比較関数 31bool compare_x(Point a,Point b) 32{ 33 return a.x<b.x; 34} 35 36bool compare_y(Point a,Point b) 37{ 38 return a.y<b.y; 39} 40 41//点集合の包囲長方形を計算する関数 42Rectangle bounding_rectangle(std::vector<Point>& points) 43{ 44 auto it; 45 it.lx=std::min_element(points.begin(),points.end(),compare_x); //左辺のx座標 46 it.rx=std::max_element(points.begin(),points.end(),compare_x); //右辺のx座標 47 it.dy=std::min_element(points.begin(),points.end(),compare_y); //下辺のy座標 48 it.uy=std::max_element(points.begin(),points.end(),compare_y); //上辺のy座標 49 return it; 50} 51 52int main() 53{ 54 int n; 55 std::cout<<"点の個数を入力してください\n"; 56 std::cin>>n; 57 58 std::vector<Point> pts; 59 pts.resize(n); 60 std::cout<<"点の座標を入力してください\n"; 61 for(Point& p:pts){ 62 std::cin>>p.x>>p.y; 63 } 64 65 std::cout<<"包囲長方形の座標は\n"; 66 67 Rectangle sq=bounding_rectangle(pts); //最小包囲長方形 68 std::cout<<sq; 69 std::cout<<"\n"; 70 71 return 0; 72}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

SHOMI

2020/07/03 18:51

> 反復子itを長方形のメンバとしたい どういうことでしょうか?
guest

回答1

0

ベストアンサー

Rectangle bouding_rectangle関数の中で以下のようなエラーが出る理由がわかりません。

autoは変数の型を初期化子から推論するので、auto it;とはできません。

diff

1Rectangle bounding_rectangle(std::vector<Point>& points) 2{ 3- auto it; 4+ Rectangle it; 5- it.lx=std::min_element(points.begin(),points.end(),compare_x); //左辺のx座標 6+ it.lx=std::min_element(points.begin(),points.end(),compare_x)->x; //左辺のx座標 7- it.rx=std::max_element(points.begin(),points.end(),compare_x); //右辺のx座標 8+ it.rx=std::max_element(points.begin(),points.end(),compare_x)->x; //右辺のx座標 9- it.dy=std::min_element(points.begin(),points.end(),compare_y); //下辺のy座標 10+ it.dy=std::min_element(points.begin(),points.end(),compare_y)->y; //下辺のy座標 11- it.uy=std::max_element(points.begin(),points.end(),compare_y); //上辺のy座標 12+ it.uy=std::max_element(points.begin(),points.end(),compare_y)->y; //上辺のy座標 13 return it; 14}

反復子itを長方形のメンバとしたい

Rectangleのメンバをstd::vector<Point>::iteratorにしたいということでしょうか?
x,y両方へアクセスできるのでバグの原因となりそうですが…

C++

1struct Rectangle 2{ 3 std::vector<Point>::iterator lx; 4 std::vector<Point>::iterator rx; 5 std::vector<Point>::iterator dy; 6 std::vector<Point>::iterator uy; 7}; 8 9//Rectangleに対する出力用の演算子 10std::ostream& operator<<(std::ostream& out, Rectangle r) 11{ 12 out << "左辺のx座標" << r.lx->x << " 右辺のx座標" << r.rx->x << "下辺のy座標" << r.dy->y << "上辺のy座標" << r.uy->y; 13 return out; 14} 15 16//点集合の包囲長方形を計算する関数 17Rectangle bounding_rectangle(std::vector<Point>& points) 18{ 19 Rectangle it; 20 it.lx = std::min_element(points.begin(), points.end(), compare_x); //左辺のx座標 21 it.rx = std::max_element(points.begin(), points.end(), compare_x); //右辺のx座標 22 it.dy = std::min_element(points.begin(), points.end(), compare_y); //下辺のy座標 23 it.uy = std::max_element(points.begin(), points.end(), compare_y); //上辺のy座標 24 return it; 25}

投稿2020/07/03 18:51

編集2020/07/03 19:01
SHOMI

総合スコア4079

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

langhtorn

2020/07/04 02:59

質問内容がわかりにくくてすみません。推測通りです。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問