vector<ofstream> indexing

G

Gary Wessle

Hi

I have a map<string, double> m_temperatures which gets updated
often.
I need to save the data to files corresponding to each string each
time the map is updated, I am expecting about 80 files in total.

how do I go about it?, do I set a vector<ofstream*> cities; something
like

map<string, double> m_temperatures;
vector<ofstream*> files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterataor i = m_temperatures.begin();
i != m_temperatures.end(), i++ ){
ofstream* ofs( i->first.c_str(), ios::app );
files.push_back( ofs );
}

void updating( map<string,double> mp ){
loop through the map and increment a counter, use this counter to
index the vector<ofstream*> which can then be de-refrenced and
values from the map appended to the file.
}

but that is fragile because the order of the map is no guaranty to
correspond to the order of the streams in the vector.

any better ideas.

thanks
 
I

isanbard

Hi

I have a map<string, double> m_temperatures which gets updated
often.
I need to save the data to files corresponding to each string each
time the map is updated, I am expecting about 80 files in total.

how do I go about it?, do I set a vector<ofstream*> cities; something
like

map<string, double> m_temperatures;
vector<ofstream*> files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterataor i = m_temperatures.begin();
i != m_temperatures.end(), i++ ){
ofstream* ofs( i->first.c_str(), ios::app );
files.push_back( ofs );
}

void updating( map<string,double> mp ){
loop through the map and increment a counter, use this counter to
index the vector<ofstream*> which can then be de-refrenced and
values from the map appended to the file.

}but that is fragile because the order of the map is no guaranty to
correspond to the order of the streams in the vector.

Slightly grosser hack:

std::pair<double, std::eek:fstream> ValPair;
std::map<std::string, ValPair> m_temperatures;

for (std::map<std::string, ValPair>::iterator i =
m_temperature.begin(),
e = m_temperature.end(); i != e; ++I) {
ofstream* ofs(i->first.c_str(), ios::app);
i->second.first = ofs;
}

void updating(std::map<std::string, ValPair> mp) {
...
}

-bw
 
C

Capstar

(e-mail address removed) schreef:
Slightly grosser hack:

std::pair<double, std::eek:fstream> ValPair;
I think you're missing a typedef here.
std::map<std::string, ValPair> m_temperatures;

for (std::map<std::string, ValPair>::iterator i =
m_temperature.begin(),
e = m_temperature.end(); i != e; ++I) {
ofstream* ofs(i->first.c_str(), ios::app);
i->second.first = ofs;
}

void updating(std::map<std::string, ValPair> mp) {
...
}

-bw

Depending on the requirements for your m_temperature, I'd probably wrap
the whole thing in a class, that will act more or less like a std::map,
but on assigning a value to a key, it would append a string to a file.
This might however not be worth while if you want this class to be a
compliant container, since then it's a quite a lot more work.

Mark
 
G

Gary Wessle

Gary Wessle said:
Hi

I have a map<string, double> m_temperatures which gets updated
often.
I need to save the data to files corresponding to each string each
time the map is updated, I am expecting about 80 files in total.

how do I go about it?, do I set a vector<ofstream*> cities; something
like

map<string, double> m_temperatures;
vector<ofstream*> files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterataor i = m_temperatures.begin();
i != m_temperatures.end(), i++ ){
ofstream* ofs( i->first.c_str(), ios::app );
files.push_back( ofs );
}

void updating( map<string,double> mp ){
loop through the map and increment a counter, use this counter to
index the vector<ofstream*> which can then be de-refrenced and
values from the map appended to the file.
}

but that is fragile because the order of the map is no guaranty to
correspond to the order of the streams in the vector.

any better ideas.

thanks

would this explain a bit better what I am trying to do?

int main(){
/* city, temperature map. */
vector<string> names;
names.push_back("a");
names.push_back("b");

map<string, ofstream*> m_os;
for( vector<string>::iterator i = names.begin();
i != names.end(); i++ ){
ofstream* p_of( (*i).c_str(), ios::app );
m_os[*i] = p_of;
}

for( int i=0; i<10; i++ ) {
m_os["a"].second << i << " ";
}
}
 
G

Gary Wessle

Capstar said:
(e-mail address removed) schreef:
I think you're missing a typedef here.


Depending on the requirements for your m_temperature, I'd probably wrap
the whole thing in a class, that will act more or less like a std::map,
but on assigning a value to a key, it would append a string to a file.
This might however not be worth while if you want this class to be a
compliant container, since then it's a quite a lot more work.

Mark

thanks for the idea.

here is as complete and simple I can put it, but it is not firing, I
am screwing up the syntax? or the structure is wrong?

this is not compiling, if I get it to compile, it will "hopefully"
solve my problem.

int main(){
vector<string> names;
names.push_back("a");
names.push_back("b");

map<string, ofstream*> m_os;
for( vector<string>::iterator i = names.begin();
i != names.end(); i++ ){
ofstream* p_of( (*i).c_str(), ios::app );
m_os[*i] = p_of;
}

for( vector<string>::iterator i=names.begin();
i!=names.end(); i++ ){
(*( m_os[*i].second)) << "123" << " ";
}
}
 
C

Capstar

Gary Wessle schreef:
here is as complete and simple I can put it, but it is not firing, I
am screwing up the syntax? or the structure is wrong?

this is not compiling, if I get it to compile, it will "hopefully"
solve my problem.

I have not tested my alterations, but I think it might work.
int main(){
vector<string> names;
names.push_back("a");
names.push_back("b");

map<string, ofstream*> m_os;
Try:map said:
for( vector<string>::iterator i = names.begin();
i != names.end(); i++ ){
ofstream* p_of( (*i).c_str(), ios::app );
Try: ofstream p_of( (*i).c_str(), ios::app );
m_os[*i] = p_of;
}

for( vector<string>::iterator i=names.begin();
i!=names.end(); i++ ){
(*( m_os[*i].second)) << "123" << " ";
}
}

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top