vector with strings and integers

Hello,
I have a file where the first column shows letters, second column shows numbers.
How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?
Last edited on
This is a completely different problem from your original thread, so the code I helped you write is not suitable.

I'd suggest scrapping that program and starting over. You will only need one vector of pairs of strings and integers:

std::vector<std::pair<std::string, int> >
Last edited on
I changed the code using pair vector but still I get errors.
Last edited on
distan.back().push_back(d);

change it to

distan.push_back(std::make_pair("name", d));
Great! First error disappear but the ones with size and binary remains.
While L B's modification will make that part of the code compile, I don't believe it's a fix. That whole section of code looks dodgy. What is the format of the file you're reading from?

What do you mean to happen on lines 30 and 32?
csv file
Last edited on
Since you don't have a 2 dimensional array, it's hard to see how they would print a two dimensional array. What you have is one array (or vector) of pairs.
Perhaps the following will help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility> // for std::pair
#include <limits>

typedef std::vector<std::pair<std::string, int>> vector_type ;

vector_type read( const std::string & filename )
{
    vector_type v ;

    std::string nodeValue ;
    int nodeLinks ;

    std::ifstream in(filename.c_str()) ;

    while ( std::getline(in, nodeValue, ',') && in >> nodeLinks )
    {
        v.push_back(std::make_pair(nodeValue, nodeLinks)) ;
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // prepare for the next getline.
    }

    return v ;
}

int main()
{
    vector_type distances = read("file.csv") ;

    for ( vector_type::iterator it = distances.begin(); it != distances.end(); ++it )
        std::cout << it->first << " : " << it->second << '\n' ;

    // or with indices:
    for ( unsigned i=0; i<distances.size(); ++i )
        std::cout << distances[i].first << " : " << distances[i].second << '\n' ;

}
Thank you very much, the code worked very well! Just wanted to ask what exactly this line do?
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

I would like to ask another question. How could I display a file containing big array of letters and numbers?
Last edited on
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
is the canonical way to discard the rest of a line.

1
2
3
4
5
    while ( std::getline(in, nodeValue, ',') && in >> nodeLinks )
    {
        v.push_back(std::make_pair(nodeValue, nodeLinks)) ;
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // prepare for the next getline.
    }


Here, for instance, there is (at least) a newline left in the stream after the extraction operations in the loop condition. If the newline were left in the stream, after the next getline, nodeValue would be "\nA" rather than just "A". Probably could've gotten away with in.ignore() ; but being explicit doesn't cost anything, and makes the code a little more forgiving if, for instance, the data file was edited by a human and an extraneous space was placed at the end if the line.

How could I include the titles of stations as appears in the spreadsheet?

I would think you'd be more concerned with how they appeared in the csv. If they don't appear in the csv, nothing is stopping you from modifying the output to resemble the spreadsheet. You may need to read and parse the first line in the file to know the number of columns you need to show, before you actually output the row of "station titles."

The output shows large numbers with minus sign.
Last edited on
Any help would be very welcome! :)
Could you post your current code? Also, could you copy and paste the output in text format and stop posting image screenshots?
Topic archived. No new replies allowed.