Solved

Storing data

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now