C++의 파일에서 읽은 값을 사용하는 데 도움이 되나요? (Some help using my values read from a file in C++?)


문제 설명

C++의 파일에서 읽은 값을 사용하는 데 도움이 되나요? (Some help using my values read from a file in C++?)

작성하려는 프로그램에 몇 가지 문제가 있습니다. 누군가 내가 직면한 문제를 처리하는 방법에 대해 약간의 지침을 줄 수 있기를 바랍니다...

여기까지 내 코드는 다음과 같습니다.

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

struct City
{
string name;
int x;
int y;
};

int main()
{
vector<City>Coord;

City entries[10];

ifstream fin("Locations.txt");

int nb_entries;

string name;

int x, y;

for(nb_entries = 0; fin.good() && nb_entries < 10; nb_entries++)

{

    fin >> entries[nb_entries].name;

    fin >> entries[nb_entries].x;

    fin >> entries[nb_entries].y;

    cout << "City: " << entries[nb_entries].name << ", ";

    cout << "x: " << entries[nb_entries].x << ", ";

    cout << "y: " << entries[nb_entries].y << ", " << endl;

}   

system("pause");

return 0;

}

텍스트 파일에 저장한 좌표를 읽어 화면에 출력합니다. 텍스트 파일의 형식은 다음과 같습니다.

city1 x1 y1

city2 x2 y2

...

city10 x10 y10

그리고 이 단계의 출력은 내 코드가 작동하는지 확인할 수 있도록 하기 위한 것이었습니다. 이제 내가 해야 할 일은 도시를 선택하는 사용자로부터 입력을 받은 후 이 좌표를 사용하는 것입니다. 좌표를 배열과 벡터로 읽으려고 시도했지만 값으로 저장하고 나중에 프로그램에서 호출하는 가장 좋은 방법을 찾기 위해 고심하고 있습니다. 저는 아직 프로그래밍과 C++에 익숙하지 않으므로 도움을 주시면 감사하겠습니다!


참조 솔루션

방법 1:

One option is to use a map from city names to coordinate pairs like below:

#include <unordered_map>

unordered_map<string, pair<int,int> >    myMap;

You can put your data into myMap like this:

myMap[cityName] = make_pair(x,y);

and can recall them like this:

x = myMap[cityName].first;
y = myMap[cityName].second;

(by Surgemask)

참조 문서

  1. Some help using my values read from a file in C++? (CC BY‑SA 2.5/3.0/4.0)

#coordinates #C++ #vector #arrays #file






관련 질문

지구의 모든 좌표를 생성하시겠습니까? (Generate all coordinates of earth?)

Spritekit이 부모를 변경하면 노드가 사라집니다. (Spritekit changing parent makes the node disappear)

C++의 파일에서 읽은 값을 사용하는 데 도움이 되나요? (Some help using my values read from a file in C++?)

Java의 캔버스에 마우스가 그리는 좌표만 화면에 인쇄하려면 어떻게 해야 합니까? (How do I only print to the screen the coordinates of what the mouse is drawing on a canvas in Java?)

좌표가 영역 내부에 있는지 감지하는 방법은 무엇입니까? (How to detect if coordinate is inside region?)

대상 사각형을 포함하여 그리드의 두 사각형 사이의 거리를 계산하는 방법 (How do I calculate the distance between two squares on a grid, including the target square)

두 좌표 배열 간의 대응 관계 찾기 (Find correspondence between 2 arrays of coordinates)

Postgres 쿼리에서 포인트 유형을 사용하는 방법 (Postgres how to use point type in query)

좌표 목록으로 작업하고 싶습니다 [Haskell] (I want to work with a list of coordinates [Haskell])

C 프로그램에서 해당 선의 특정 점에서 특정 거리, 선 위의 점 찾기 (Find a point on a line, a certain distance from a certain point on that line in c program)

점이 있는 geopandas 데이터 프레임에서 다각형 만들기 (Creating a polygon from a geopandas dataframe with points)

Netlogo는 특정 xycor를 설정합니다. (Netlogo set specific xycor)







코멘트