#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Need help to search vector arrays...


    How would I search within a vector array for a user input? I need to find the city in the vector array and use it's index to compare distances between other arrays with the array. This is what I have so far, I've read the cvs file to a vector array and now i need to search but i'm lost. HELP WOULD BE APPRECIATIVE. Thanks

    #pragma warning(disable: 4786) // VC++ 6.0 disable warning about debug line too long
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <fstream>
    #include <functional>
    #include <algorithm> // std::find
    using namespace std;
    typedef vector<string> LINE;


    int main()
    {
    string line;
    int pos;

    vector<LINE> array;

    ifstream in("worldcities.csv");
    if(!in.is_open())
    {
    cout << "Failed to open file" << endl;
    return 1;
    }
    while( getline(in,line) )
    {
    LINE ln;
    while( (pos = line.find(',')) >= 0)
    {
    string field = line.substr(0,pos);
    line = line.substr(pos+1);
    ln.push_back(field);
    }
    ln.push_back(line);
    array.push_back(ln);
    }

    //for (int i = 0; i < array.size (); i++)
    //{
    //vector <string> cityData = array.at(i);
    //for (int j = 0; j < cityData.size(); j++)
    //{
    // cout << cityData.at(j) << ",";
    //}
    //cout << endl;
    //}


    string input;
    do
    {
    cout << "The worldcities.cvs contained infor on 120 cities." << endl;
    cout << "Enter a city by name or number (1-120)" << endl;
    std::getline (std::cin,input);




    if(false)
    {
    cout << "I don't understand your input" << endl;
    continue;
    }
    }while (true);

    system("pause");
    return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Also posted here.

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo