473,376 Members | 1,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

vector<ofstream> indexing

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
5 3739
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
3
by: Juicer_X | last post by:
Hello everyone, I've been working with the STL Containers for a little while now, but in the middle of working on a small "Markov Chain" class I realized that I wanted to modify my frequency...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
6
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++)...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.