#include <vector>
#include <map>
usingnamespace std;
int main() {
vector< map< int, double>* > M;
M.resize( 10 );
for( int i=0; i < M.size(); ++i ) {
M[ i ] = new map<int,double>;
M[ i ][ i ] = 0.0; <<<<<<<<<< error
}
}
Produces:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
junk.cpp(12) : error C2679: binary '=' :
no operator found which takes a right-hand operand of type 'double'
(or there is no acceptable conversion)
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\Include\map(176):
could be 'std::map<_Kty,_Ty> &std::map<_Kty,_Ty>::operator
=(const std::map<_Kty,_Ty> &)'
with
[
_Kty=int,
_Ty=double
]
while trying to match the argument list '(std::map<_Kty,_Ty>, double)'
with
[
_Kty=int,
_Ty=double
]
That couldn't possibly work in verbosity oriented language like C++ :)
If your C++ is verbose, you're either writing something complicated and unusual, or you're "doing it wrong", as they say. It's common for beginners to write excessive code because they haven't yet learned the standard library.
If your C++ is verbose, you're either writing something complicated and unusual, or you're "doing it wrong", as they say
Hm. That was mostly humour, but since you took it seriously...
I'm trying to optimise some code that does some heavy work with sparse matrix.
The original code uses a hand-code linked list for the sparse part of the matrix; and I thought I try replacing it with a "tried and tested" library routine, using maps.
This code from the original (circa 2000) code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void CBigLinProb::MultA( valarray<double> &X, valarray<double> &Y ) {
constint nn = n;
CEntry *e;
for( int i=0; i < nn; i++ ) Y[ i ] = 0;
for( int i=0; i < nn; i++ ){
Y[ i ] += M[ i ]->x * X[ i ];
e = M[ i ]->next;
while( e != NULL ) {
Y[ i ] += e->x * X[ e->c ];
Y[ e->c ] += e->x * X[ i ];
e = e->next;
}
}
}
became:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void CBigLinProb::MultA( valarray<double> &X, valarray<double> &Y ) {
for( int i=0; i < MM.size(); i++ ) Y[ i ] = 0;
for( int i=0; i < MM.size(); i++ ){
map<int,double> s = MM[ i ];
Y[ i ] += s[ i ] * X[ i ];
for( map<int,double>::iterator it=s.begin(); it != s.end(); ++it ) {
Y[ i ] += it->second * X[ it->first ] * LAMBDA;
Y[ it->first ] += it->second * X[ i ];
}
}
}
And the result is that the original run time of 11.473 seconds became 166.939 seconds.
Yes. I know that with C++11, those loop iterator definitions can be simplified with auto; but C++11 isn't an option; and it doesn't simplify it that much.
Your passive-aggressive tone is childish and pathetic. Grow up. If you're capable of genuine discussion, do it. If not, take it back to the playground.