473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<ofstream > indexing

Hi

I have a map<string, doublem_tempera tures 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_tempera tures;
vector<ofstream *files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterat aor 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,doub lemp ){
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 3766
On Dec 12, 5:22 pm, Gary Wessle <phd...@yahoo.c omwrote:
Hi

I have a map<string, doublem_tempera tures 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_tempera tures;
vector<ofstream *files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterat aor 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,doub lemp ){
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<doubl e, std::ofstreamVa lPair;
std::map<std::s tring, ValPairm_temper atures;

for (std::map<std:: string, ValPair>::itera tor i =
m_temperature.b egin(),
e = m_temperature.e nd(); i != e; ++I) {
ofstream* ofs(i->first.c_str( ), ios::app);
i->second.first = ofs;
}

void updating(std::m ap<std::string, ValPairmp) {
...
}

-bw

Dec 13 '06 #2
is******@gmail. com schreef:
On Dec 12, 5:22 pm, Gary Wessle <phd...@yahoo.c omwrote:
> Hi

I have a map<string, doublem_tempera tures 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_tempera tures;
vector<ofstrea m*files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterat aor 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,doub lemp ){
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<doubl e, std::ofstreamVa lPair;
I think you're missing a typedef here.
std::map<std::s tring, ValPairm_temper atures;

for (std::map<std:: string, ValPair>::itera tor i =
m_temperature.b egin(),
e = m_temperature.e nd(); i != e; ++I) {
ofstream* ofs(i->first.c_str( ), ios::app);
i->second.first = ofs;
}

void updating(std::m ap<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.c omwrites:
Hi

I have a map<string, doublem_tempera tures 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_tempera tures;
vector<ofstream *files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterat aor 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,doub lemp ){
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<stringna mes;
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.c omwrote:
Hi

I have a map<string, doublem_tempera tures 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_tempera tures;
vector<ofstream *files

/* first create ofstreams and leave them open for appending */
for( map<string, double>::iterat aor 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,doub lemp ){
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<doubl e, std::ofstreamVa lPair;
I think you're missing a typedef here.
std::map<std::s tring, ValPairm_temper atures;

for (std::map<std:: string, ValPair>::itera tor i =
m_temperature.b egin(),
e = m_temperature.e nd(); i != e; ++I) {
ofstream* ofs(i->first.c_str( ), ios::app);
i->second.first = ofs;
}

void updating(std::m ap<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<stringna mes;
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<stringna mes;
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
5046
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 it to clear the temp vector for each file. ********************* code *******************
1
2125
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 stdafx.h, I found that to compile i had to change <fstream.h> to <fstream> to get it to compile.
3
10816
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 tables, so far I have been using maps like: std::map<std::string, int> frequency; A string for the letter combinations and an int for the...
15
6922
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 list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all...
13
3393
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. The first column is always an English
6
2742
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++) { write(ostr, (char *) Output_Buffer, 1);
2
7259
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
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.