Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

im trying to map strings from a data file using c++, my problem is ive never used the map data structure before so im not

im trying to map strings from a data file using c++, my problem is ive never used the map data structure before so im not sure what im doing

i need to read the strings in from this data file but ignore duplicate strings

the data file is formated like this:(except its about 750 lines long)

From: Seoul, South Korea To : Toronto, Canada Luxembourg, Luxembourg Seattle, United States Dhaka, Bangladesh Guatemala City, Guatemala From: Toronto, Canada To : Ottawa, Canada Luxembourg, Luxembourg Guatemala City, Guatemala

so far ive managed to code it so it reads all of the strings into an array but i cant get it to ignore the duplicates and i was told i could do so using maping

this is the code

int main () { int b = 0; map cities; string arr[2500]; // setting the max no. of city in file = 100

string line; // opening the file in reading mode

ifstream myfile ("connections.txt");

int pos = 0; // for indexing in our string array

if (myfile.is_open()) // if file gets open {

while ( getline (myfile,line) ) // getting line by line data { string temp=""; // to store string temporarily

for(int i=0; i<=line.length(); i++) // traversing till end of string

{

if(i==line.length()) // As last character can be ' '

{

if(temp.size()>=1) // if there is any string in temp { arr[pos++] = temp; temp=""; continue; }

}

if(line[i]==' ') continue; // if space then continue

if(isalpha(line[i])) temp+=line[i]; // if it is alphabet then add to temp

if(line[i]==':') temp=""; // if it is ':', then initialize the temp string again

if(line[i]==',') // if it is ',' , then store it to the array

{ temp += line[i]; temp += line[i+1];

}

} //if (!isDuplicate(arr, temp, b)) //{ cities.insert(pair (b, temp) ); arr[pos++] = temp; temp =""; b++; //} //else //{ //temp =""; // } //temp ="";

}

myfile.close(); // closing the file map :: iterator i; int p = 0; for (i = cities.begin(); i != cities.end();++i) { if (p%5 == 0) { getch(); } p++; cout << '\t' << i->first; cout << '\t' << i->second << ' '; } //for(int i=0; i //{

//cout< //}

}

else // if file doesn't get open

cout << "Unable to open file";

return 0;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

Students also viewed these Databases questions

Question

What did they do? What did they say?

Answered: 1 week ago