Click to See Complete Forum and Search --> : Question about allocate of STL.


George2
February 13th, 2003, 07:03 AM
Hi, everyone!

Here is a sample of the usage of allocate of STL.
When compiling it with VC6.0, some error occur.

I have added the source codes and related error
messages below.

Source:

--------
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;

int main()
{
vector<int> a (2, 5);
vector<int> b (2, 7);
int *c = allocate((ptrdiff_t) a.size(), (int*)0 );

transform ( a.begin(), a.end(), b.begin(),
raw_storage_iterator<int*, int> (c), plus<int>() );

copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );

return 1;
}
--------


Error messages:

--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testRawIterator\testRawIterator.cpp(12) : error C2065: 'allocate' :

undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\testRawIterator\testRawIterator.cpp(12) : error C2440: 'initializing' :

cannot convert from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
--------


How to resolve the trouble?


Thanks in advance,
George

Philip Nicoletti
February 13th, 2003, 07:34 AM
see comments in code below:


#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <memory> // **** might need on some systems

using namespace std;

int main()
{
vector<int> a (2, 5);
vector<int> b (2, 7);

allocator<int> my_memory; // *******

int *c = my_memory.allocate( a.size(), (int*)0 ); // *******

transform ( a.begin(), a.end(), b.begin(),
raw_storage_iterator<int*, int> (c), plus<int>() );

copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );


my_memory.deallocate(c,a.size()); // *******

return 1;
}

George2
February 13th, 2003, 07:39 AM
Hi, Philip buddie!

I have tried your code and it works OK!

I still have a question, one of the feature of raw_storage_iterator
is that it can put results directly into uninitialized memory. Can I see this feature in this sample?


Thanks in advance,
George

George2
February 13th, 2003, 09:21 PM
Thanks, Philip buddie!

George

Originally posted by Philip Nicoletti
see comments in code below:


#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <memory> // **** might need on some systems

using namespace std;

int main()
{
vector<int> a (2, 5);
vector<int> b (2, 7);

allocator<int> my_memory; // *******

int *c = my_memory.allocate( a.size(), (int*)0 ); // *******

transform ( a.begin(), a.end(), b.begin(),
raw_storage_iterator<int*, int> (c), plus<int>() );

copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );


my_memory.deallocate(c,a.size()); // *******

return 1;
}