Solved

Storing data

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

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

737 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