By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

vector<ofstream> indexing

P: n/a
Hi

I have a map<string, doublem_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, doublem_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,doublemp ){
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
Dec 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Dec 12, 5:22 pm, Gary Wessle <phd...@yahoo.comwrote:
Hi

I have a map<string, doublem_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, doublem_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,doublemp ){
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::ofstreamValPair;
std::map<std::string, ValPairm_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, ValPairmp) {
...
}

-bw

Dec 13 '06 #2

P: n/a
is******@gmail.com schreef:
On Dec 12, 5:22 pm, Gary Wessle <phd...@yahoo.comwrote:
> Hi

I have a map<string, doublem_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, doublem_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,doublemp ){
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::ofstreamValPair;
I think you're missing a typedef here.
std::map<std::string, ValPairm_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, ValPairmp) {
...
}

-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
Dec 13 '06 #3

P: n/a
Gary Wessle <ph****@yahoo.comwrites:
Hi

I have a map<string, doublem_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, doublem_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,doublemp ){
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<stringnames;
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 << " ";
}
}

Dec 13 '06 #4

P: n/a
Capstar <ne**@eg.homeip.netwrites:
is******@gmail.com schreef:
On Dec 12, 5:22 pm, Gary Wessle <phd...@yahoo.comwrote:
Hi

I have a map<string, doublem_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, doublem_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,doublemp ){
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::ofstreamValPair;
I think you're missing a typedef here.
std::map<std::string, ValPairm_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, ValPairmp) {
...
}

-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
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<stringnames;
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" << " ";
}
}

Dec 13 '06 #5

P: n/a
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<stringnames;
names.push_back("a");
names.push_back("b");

map<string, ofstream*m_os;
Try:map<string, ofstreamm_os;
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
Dec 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.