Re: [Dev-C++] How to work with *.txt files??
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2004-09-18 21:50:57
|
ZwiQ wrote:
> I assume that assigning pointers isn't as resourcesconsuming as
> assigning strings?
Most STL implementations (including GCC) provide an efficient way to
copy a string. In Stroustrup's book he illustrates a basic string
implementation this way.
Try it youself:
----
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int n = 10000000; // just any big number
void strings_test()
{
cout << "Strings test, press enter to start" << endl;
cin.ignore();
string msg =
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
list<string> foo;
for (int i=0; i<n; i++)
foo.push_back(msg);
cout << "Press enter to free " << n << " strings" << endl;
cin.ignore();
}
void int_test()
{
cout << "Int test, press enter to start" << endl;
cin.ignore();
list<int> foo;
for (int i=0; i<n; i++)
foo.push_back(1);
cout << "Press enter to free " << n << " numbers" << endl;
cin.ignore();
}
int main()
{
strings_test();
int_test();
return 0;
}
----
While you execute this program, keep watching the memory your program
consumes. You'll see that copying strings doesn't duplicate the data,
they share the same representation until you change one of them. With
this program you can also compare the size alocated by both the string
list and an int list.
Daniel K. O.
|