|
dear friend
where can i find ofs.write() and ofs.read() function explanation? thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
|
dear brother
i tried many times i still couldn't finish this point, could you help me to provide detail of this. how to write 10 3x4 random value arrays, thanks a lot..i am really worried now.....i couldn't extend from your previious code
Li Zhiyuan
5/10/2006
|
|
|
|
|
But do you need to write in a human readable format (i.e. such that you could later open the file with the Notepad and see the numbers) or you need just to store the numbers for later retrieval (and re-construction of the array)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
no need, just create 10 3x4 arrays, and in later my program, i need to use anyone of them...that is all, thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
If I remember well, you have arrays of doubles.
I give you a really crude sample code working.
(1) Create 10 3x4 arrays, initialise them with random values, i.e.
int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
for (j=0; j<3; j++)
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
(2) Store all of the elements of all the arrays inside a file, explicitely
exploiting our knowledge of array size (really poorly coding, indeed).
FILE * fp = fopen("arrays.bin","wb");
fwrite(a, sizeof(double), 10 * 3 * 4, fp);
fclose(fp);
(3) Retrieve all of the array values from file content to buil the new 10 3x4 arrays.
double b[10][3][4];
FILE * fp = fopen("arrays.bin","rb");
fread(b, sizeof(double), 10 * 3 * 4, fp);
fclose(fp);
(3) bis, retrieve the 7-th array from the file content
double c[3][4];
FILE * fp = fopen("arrays.bin","rb");
fseek(fp, sizeof(double) * 6 * 4 * 3, SEEK_SET);
fread(c, sizeof(double), 3 * 4, fp);
fclose(fp);
hope that helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
thanks a lot brother, how can i see a[5][3][4] array?
int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
{
for (j=0; j<3; j++)
{
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
cout <<" "<<a[i][j][0]<<" "<<a[i][j][1]<<" "<<a[i][j][2]<<" "<<a[i][j][3];
cout << endl;
}
}
but however i couldn't get solution, for example,
a[5]
display 3x4 array
like this.
Li Zhiyuan
5/10/2006
|
|
|
|
|
li zhiyuan wrote: how can i see a[5][3][4] array?
please elaborate.
li zhiyuan wrote: int i, j, k;
double a[10][3][4];
for (i=0; i<10; i++)
{
for (j=0; j<3; j++)
{
for (k=0; k<4; k++)
a[i][j][k] = (double)rand()/RAND_MAX;
cout <<" "<<a[i][j][0]<<" "<<a[i][j][1]<<" "<<a[i][j][2]<<" "<<a[i][j][3];
cout << endl;
}
}
but however i couldn't get solution, for example,
a[5]
display 3x4 array
Your code displays all of the a elements. What do you need instead?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
li zhiyuan wrote: how to create 10 3x4 arrays...
Creating arrays and printing them are two different things. Which one is troubling you?
li zhiyuan wrote: how can i find full explanation of format specifier: &d,%1f........i need to read recall them..forgot it already..thanks a lot
CPallini already provided you with a link. Did you not consult it?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
With std::transform , how to transform the stream buffer of an std::fstream object to lower case? Thanks!
The below code gets error C2248.
std::fstream file("data.txt", std::ios::in);
std::istream_iterator<std::fstream> p(file);
std::istream_iterator<std::fstream> pE;
std::transform(p, pE, p, ::tolower);
Maxwell Chen
|
|
|
|
|
I found the solution!
std::fstream file("data.txt", std::ios::in);
std::string s(std::istreambuf_iterator<char>(file.rdbuf()), std::istreambuf_iterator<char>());
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
Maxwell Chen
|
|
|
|
|
This worked for me:
#include "stdafx.h"
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char* argv[])
{
using namespace std;
cin >> noskipws;
istream_iterator<char> isi(cin);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(cout);
transform(isi, isi_end, osi, ::tolower);
return 0;
}
Steve
|
|
|
|
|
Stephen Hewitt wrote:
cin >> noskipws;
istream_iterator<char> isi(cin);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(cout);
transform(isi, isi_end, osi, ::tolower);
But when I replace cin and cout with a single fstream object, it won't work.
istream_iterator<char> isi(file);
istream_iterator<char> isi_end;
transform(isi, isi_end, isi, ::tolower);
Maxwell Chen
|
|
|
|
|
The third argument should be osi , not isi : isi is an input iterator.
Steve
|
|
|
|
|
Stephen Hewitt wrote: The third argument should be osi, not isi: isi is an input iterator.
But then it is not working. I am not sure if any mistake below...
ABC DEF
using namespace std;
fstream file("data.txt", ios::in);
file >> noskipws;
istream_iterator<char> isi(file);
istream_iterator<char> isi_end;
ostream_iterator<char> osi(file);
transform(isi, isi_end, osi, ::tolower);
file.clear();
file.seekg(0, file.beg);
string s;
while(!file.eof()) {
getline(file, s);
cout << s << end;
}
Output does not have any change though.
ABC DEF
Maxwell Chen
|
|
|
|
|
Try something like this then:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>
int main(int argc, char* argv[])
{
using namespace std;
ifstream ifs("C:\\Data.txt");
if (!ifs)
{
cerr << "Failed to open data file!" << endl;
return 1;
}
istream_iterator<char> isi(ifs);
istream_iterator<char> isi_end;
string s;
transform(isi, isi_end, back_inserter(s), ::tolower);
cout << s << endl;
return 0;
}
NOTE: I couldn't test this as MSVC6's std::string class is missing the push_back member function. Section 21.3.5 of the C++ standard says it should exist.
Steve
|
|
|
|
|
Stephen Hewitt wrote: transform(isi, isi_end, back_inserter(s), ::tolower);
Then this solution is very much similar to my previous one (URL[^]). Both of them copy the file buffer to a string. The only difference is that your solution requires to do file >> noskipws; in addition because file.rdbuf() keeps the spaces and the newlines whereas another way does not.
Maxwell Chen
|
|
|
|
|
Hi all,
May this question can be odd to you, if so I'm really sorry.
I've write a simple server application which is used to make a chat window. Actually there is two chat windows on the same HTML page. Whatever I type on one window is displayed on the other. Now I want to automatically run the server, without pressing F5 and start, at predefined time. I think it is clear to you.
One of my friend says that, using global mutex, I think it is the right word I can do that. But I don't know anything about it. Can you guys give me a help on it.
Thanks a lot...
I appreciate your help all the time...
Eranga
|
|
|
|
|
I have an std::map and I want to use std::for_each and boost::bind to call a method on the 'second' object in each map node. Anyone know the correct syntax?
class MyType
{
public:
void Method()
{
}
};
std::map<int, MyType> aMap;
this is what I can get right...
std::for_each(aMap.begin(), aMap.end(), boost::bind(&MyType::Method))
|
|
|
|
|
This works for me:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <functional>
#include <boost/bind.hpp>
using namespace std;
class MyType
{
public:
MyType(const string &name) : m_name(name) {}
void Method()
{
cout << m_name << endl;
}
private:
string m_name;
};
typedef std::map<int, MyType> Map_t;
int main(int argc, char* argv[])
{
Map_t m;
m.insert(Map_t::value_type(1, MyType("One")));
m.insert(Map_t::value_type(2, MyType("Two")));
m.insert(Map_t::value_type(3, MyType("Three")));
m.insert(Map_t::value_type(4, MyType("Four")));
m.insert(Map_t::value_type(5, MyType("Five")));
for_each(m.begin(), m.end(), boost::bind(&MyType::Method, boost::bind(&Map_t::value_type::second, _1)));
return 0;
}
Steve
|
|
|
|
|
Thanks mate
Someone here pointed out to me that this is not an efficient way of doing this as the bind is called once for each item in the sellection.
|
|
|
|
|
Josh Gray wrote: Someone here pointed out to me that this is not an efficient way of doing this as the bind is called once for each item in the sellection.
Perhaps... I haven't looked at the generated machine code in a release build so I can't say with certainty. You’d hope that on a decent compiler the function calls would be optimized away as they’re inline.
Nevertheless, that’s what’s required to use boost::bind in the situation you described.
Steve
|
|
|
|
|
Hi,
I am trying to store a large set of "words," with "definitions," using a trie structure. The code will be written as a DLL, but I want this table to be static so that the table doesn't have to be reread every time a function from this DLL is called. The problem I have is the size of the table. When I create the object, the dictionary, used to hold these words, I can only create a fraction of the number of nodes that are needed. Is there any way I can write a container class so that each member, or set of members, of the class resides at a different location in memory? That way I can avoid the current problem of not having a contiguous block of memory large enough to hold my object.
Alternatively another option is to make multiple dictionary objects. However I have two problems with this approach.
1) Since I need the object to be globally static, I have no way of dynamically creating multiple objects. I would have to pre-declare them and use them as needed. That would suck up memory and is in general fairly messy.
2) Another problem is the matter of efficiency. The more nodes I can fit into one object, the more efficient the storage. One object containing 1M nodes can store more than 2 objects of 500,000 nodes.
Thanks.
|
|
|
|
|
gluballs wrote: That way I can avoid the current problem of not having a contiguous block of memory large enough to hold my object.
How about std::map?
|
|
|
|
|
I experimented with using maps for this application and I remembered it was not very efficient when dealing with a large number of keys (I have a table of approx 40M words). Would maps not have this memory allocation problem? For example, can I keep appending to a map object if it's memory exceeds the largest contiguous memory block on my machine?
|
|
|
|
|