Solved

Storing data

Posted on 2004-04-25
1
233 Views
Last Modified: 2010-04-01
What would be the best way to store this information???

char cases[] = "asdf"         "17"            "TESTAA"
char cases2[]= "basdf"      "25"            "STESTAB"
int cases3 = 1                   "22"            "SSSTTT"

the last column is the header info which is stored off to a file.  The 2nd to last column is the search key I use to find the value of cases...etc...

I need to store the value and the last column to a file.  

vector, struct??
0
Comment
Question by:jewee
1 Comment
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 10911139
This should give a kick-start:
--------8<--------
#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <algorithm>

struct Record {
        std::string field_1;
        int field_2;
        std::string field_3;

        Record(const std::string& field_1,int field_2,const std::string& field_3) : field_1(field_1),field_2(field_2),field_3(field_3) {}
        Record() {}
};

class RecordLocator {
        int field_2;
public: RecordLocator(int field_2) : field_2(field_2) {}
        bool operator()(const Record& r) {return r.field_2 == field_2;}
};

inline bool operator<(const Record& lhs,const Record& rhs)
{
        return lhs.field_2 < rhs.field_2;
}

inline std::ostream& operator<<(std::ostream& os,const Record& r)
{
        return os << r.field_1 << '\t' << r.field_2 << '\t' << r.field_3;
}

int main()
{
        typedef std::list<Record> RList;
        RList l;

        // Load records in unsorted order
        l.push_back(Record("asdf",17,"TESTAA"));
        l.push_back(Record("basdf",25,"STESTAB"));
        l.push_back(Record("whatever",22,"SSSTTT"));

        // Sort the list
        l.sort();

        // Display the records
        copy(l.begin(),l.end(),std::ostream_iterator<Record>(std::cout,"\n"));

        // Look for the record with field_2 == 22
        RList::const_iterator itr = find_if(l.begin(),l.end(),RecordLocator(22));
        if (itr != l.end())
                std::cout << "Found 22: " << *itr << '\n';
        else
                std::cout << "22 not found\n";
}
--------8<--------
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question