English 中文(简体)
STL图书馆界定了分法算法
原标题:define dijkstra algorithm by STL library

here code for dijkstra algorithm by using macros and STL library

#include<iostream>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<queue>
#include<algorithm>
#include<utility>
using namespace std;
vector<vector<pair<int,int> > >G;
/*
  definitions:
  G.size-number of vertices
  G[i].size() is number of vertices directly reachable from vertex with index i
  G[i][j].first  is index of j-th vertex reachable from vertex i
  G[i][j].second  is length of edge heading  from vertex to vertex G[i][j].first
*/

#define N 6
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii>vvii;
#define size(a) int ((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,it) for(typeof((c).begin() it=(c).begin();it!=(c).end();it++));
#define present(c,x) ( (c).find(x)!=(c).end())
#define cpresent(c,x) (find(all(c),x)! =(c).end())
typedef vector<int>vi;
priority_queue<ii,vector<ii>,greater<ii> >Q;
vi D(N,987654321);

void dijkastra(){
    D[0]=0;
    Q.push(ii(0,0));
    while(!Q.empty()){
        ii top=Q.top();
        Q.pop();
        int v=top.second,d=top.first;
        /*
         // this check is very important
        // we analyze each vertex only once
        // the other occurrences of it on queue (added earlier) 
        // will have greater distance
                            */
        if(d<=D[v]){
            //iterate through all outcoming edge from v
            tr(G[v],it){
            }
        }
    }
}

int main(){
    return 0;
}

When I compile it, it gives me the following errors:

macros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2146: syntax error : missing  )  before identifier  it 
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C3861:  typeof : identifier not found
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2065:  it  : undeclared identifier
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2065:  it  : undeclared identifier
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2143: syntax error : missing  ;  before  ) 
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2143: syntax error : missing  ;  before  ) 
>c:usersdatodocumentsvisual studio 2010projectsallmacros_disktraallmacros_disktraallmacros_disktra.cpp(49): error C2059: syntax error :  ) 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不理解错误在哪里。

最佳回答

问题在于:

#define tr(c,it) for(typeof((c).begin() it=(c).begin();it!=(c).end();it++));

<>代码>(>没有对应的>>

You might also want to consider using typedef for things like iterators and complex STL types i.e.

typedef vector<pair<int,int> > vpairs;
typedef vector<vpairs > vvpairs;
typedef vpairs::iterator vp_iter;
typedef vvpairs::iterator vvp_iter;
vvpairs G;

And if you really want to use macros then:

#define tr_vp(c,it) for(vp_iter it=(c).begin();it!=(c).end();it++)
#define tr_vvp(c,it) for(vvp_iter it=(c).begin();it!=(c).end();it++)
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签