?
Solved

Storing data

Posted on 2004-04-25
1
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
rstaveley earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

764 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