|
-
February 13th, 2003, 08:03 AM
#1
Question about allocate of STL.
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
-
February 13th, 2003, 08:34 AM
#2
see comments in code below:
Code:
#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;
}
-
February 13th, 2003, 08:39 AM
#3
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
-
February 13th, 2003, 10:21 PM
#4
Thanks, Philip buddie!
George
Originally posted by Philip Nicoletti
see comments in code below:
Code:
#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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|