Originally posted by: SystemAdmin
Containers like IloNumArray were introduced to the Concert API a
long time ago. At this time not all compilers/platforms had support for STL.
They are also different from STL containers as they are only handle-classes. This means that they do not actually hold the data but only a handle to that data. I.e., the IloXXXArray classes are only reference to the real data. Consequently, some code like this
IloNumArray X(env); X.add(1); X.add(2); X.add(3); X.add(4); IloNumArray Y = X; std::cout <<
"X: " << X << std::endl <<
"Y: " << Y << std::endl; X.remove(0); std::cout <<
"X: " << X << std::endl <<
"Y: " << Y << std::endl;
will always have the same output for X and Y. The operator= does not create a deep copy of the array. Instead it just creates a copy of the reference to the real data.
As you have noticed by now, the IloXXXArray classes are not compatible with the STL algorithms, mainly because they do not provide iterators and almost all STL algorithms are based on iterators. Instead of writing a function that converts an IloNumArray to a vector you should either write your own version of max_element() for IloNumArray or implement an iterator for that class. For example, the following minimalistic implementation does work:
#include <vector> #include <iostream> #include <algorithm> #include <ilcplex/ilocplex.h>
/** An iterator for IloNumArray instances. * The implementation assumes that you never mix iterators for different * arrays. */
class IloNumArrayIterator
{ IloNumArray array; IloInt pos;
public: IloNumArrayIterator(IloNumArray const& a) : array(a), pos(0)
{
} IloNumArrayIterator(IloNumArray const& a, IloInt p) : array(a), pos(p)
{
} bool operator!=(IloNumArrayIterator it)
const
{
return pos != it.pos;
} bool operator==(IloNumArrayIterator it)
const
{
return pos == it.pos;
} IloNum operator*()
const
{
return array[pos];
} IloNumArrayIterator& operator++()
{ ++pos;
return *
this;
}
};
int main(
void)
{ IloEnv env; IloNumArray X(env); X.add(3); X.add(5); X.add(1); X.add(6); X.add(4); std::cout <<
"Max element: " << *std::max_element(IloNumArrayIterator(X), IloNumArrayIterator(X, X.getSize())) << std::endl;
return 0;
}
#CPLEXOptimizers#DecisionOptimization