Solved

Storing data

Posted on 2004-04-25
1
232 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 170
is twain_32.dll cmpatible with windows 10 ? 10 155
FMX TCameraComponent Problem 2 68
Issues with C++ Class 19 92
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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

806 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