How to create vector of vector to handle data list

I want to use a vector of vector for my data handling in c++. The data str uctre which i want to create is like:
1 234,345, 342, 12
2 12, 321, 210
3 108, 23,234, 23, 12
so i have PointNumber and PointList of each pointnumber. How to create a vector of vector to holding these values. Do i need to cretae class ?
vector<int,vector<int> >My_points(){int PointNumber, vector<int>PointList};
is it correct? Please help me to define this correct way.

Is it possible to add data into vector from a array? ( my PointList is a array )
Last edited on
For converting the ASCII into binary, I'm guessing you mean into binary representation of these floating-point numbers. With that assumtion in mind, here is some pseudo code to think about:

Create a structure of five floating-point numbers such as this:

1
2
3
4
5
6
7
8
9
10
11

typedef struct _FiveFloats
    {
    float a;
    float b;
    float c;
    float d;
    float e;
    }
       FiveFloats;


Writing ASCII to binary:

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

open input file A.txt for reading (and check for success)

open output file B.dat for writing (and check for success)

while not the eof of the input file:
    {
    read a line of text

    if eof of the input file,
        break;

    for each number in the line:
        {
        convert the number to a floating-point number (say with atof() )

        assign the floating-point number to one of the fields in the structure (I'll leave this up to you to figure out)

        }

    write the structure to the output file.

    }

close the input file

close the output file

 


Writing binary to ASCII:

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

open input file B.dat for reading (and check for success)

open output file C.txt for writing (and check for success)

while not the eof of the input file:
    {
    read in the structure

    if eof of the input file,
        break;

    for each value in the structure:
        {
        convert the number to an ASCII string (say with sprintf() )

        write the ASCII string to the output file.

        }

    write a newline to the output file.


    }

close the input file

close the output file
Keep in mind that once you start write()ing floating point numbers to file you open yourself up to representation and endianness issues.

You might want to say, for example, that the file contains IEEE 754-1985 Single Precision values in Little-Endian byte order. (This makes it easy to just load and dump the values on a PC, but should require your code to recognize when it is being used on other systems...)

Good luck!
Thank you very much kooth, but still I am slugging since I am very poor in c++ programming. But I tried to write c++ codes according to your procedure and comments. However I could not complete it and have several mistakes in my codes (see below). Especially I don’t know that how to write codes for the convertion part (for each number in theline:
{
convert the number to a floating-point number (say with atof() )
assign the floating-point number to one of the fields in the structure (I'll leave this up to you to figure out)
}
write the structure to the output file. })

could you pls help me to complete Ascii to binary part, then hopefully I can try binary to ascii part? So I needs help to write c++ codes. Many thanks


#include <iostream>
#include <fstream>

int main (void)
{

typedef struct _FiveFloats
{
float a;
float b;
float c;
float d;
float e;
}
FiveFloats;

ifstream InTextFile;
InTextFile.open(“A.txt”);

if (!InTextFile.is_open())
{
cout << "Unable to open file, Error opening input file!!!" << endl;
}

ofstream OutBinFile;
OutBinFile.open(“B.dat”, ios_base::binary);

if (!OutBinFile.is_open())
{
cerr << "Sorry, Error creating output file!!!" << endl;
}

while (!InTextFile.eof()){
getline (InTextFile,line);

if (InTextFile.eof()){
break;
}
for
---------------- helps to write codes for this part-------------------

}


OutBinFile.close;
InTextFile.close;
}
Look at the string class for ideas about finding each number in the line. Also, look here:
http://www.linuxforums.org/forum/programming-scripting/28655-c-strings.html for a great function for solving this part.

Once you parse out a number:
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
40
41
42
43
44
45
46
47
48
49

FiveFloats Five;

int i = 0;

string
    AString,
    Line;

while( ! InTextFile.eof() }
    {
    getline( InTextFile, Line );

    if( InTextFile.eof() )
        break;

    /*    YOU FIGURE OUT HOW TO PARSE THE LINE :)    */

    for each field (let's say we assign it to AString):
        {
        AString = YourParsingFunction( i );

        float aFloat = atof( AString.data() );

        switch( i )
            {
            case 0:
                Five.a = aFloat;

                break;

            case 1:
                Five.b = AFloat;

                break;

            ( And so forth )

            }    /*    switch( i )    */

        }   /*    for( each field in the line )   */

    OutBinFile.write( ( const char * )&Five, sizeof( Five ) );

    }    /*    while( ! InTextFile.eof() )    */



 
Topic archived. No new replies allowed.