帮我将条目插入C ++标准地图

时间:2011-01-24 01:39:07

标签: c++ map

map<int, BasicBlock*> basicBlocks; // in my header file
basicBlocks.insert(std::pair<int, BasicBlock*>(pc, bb)); 

其中pc是整数,bb是BasicBlock :(早期)

BasicBlock *bb = new BasicBlock(pc); 

来自代码中的较早版本。

我收到此错误:错误C2664:'std :: pair&lt; _Ty1,_Ty2&gt; :: pair(const std :: pair&lt; _Ty1,_Ty2&gt;&amp;)':无法将参数1从'BasicBlock *'转换为'const std :: pair&lt; _Ty1,_Ty2&gt; &安培;'

Why would it even need to convert the parameter? 



    void ConstantPoolParser::createBasicBlocks(Method* method)
    {
    cout << "Creating basic block's in method: " << method->getName() << endl;
    char* bytecode = method->getBytecode();
    int bytecodeLength = method->getBytecodeLength();
    int pc = 0;
    basicBlocks.insert(new BasicBlock(pc));

    for(pc = 0; pc < bytecodeLength; pc++)
    {
        int opcode = bytecode[pc] & 0xFF;
        switch(opcode)
        {
            case IF_ICMPEQ: // 159 (0x9f) eq
            case IF_ICMPNE: // 160 (0xa0) ne
            case IF_ICMPLT: // 161 (0xa1) lt
            case IF_ICMPGE: // 162 (0xa2) ge
            case IF_ICMPGT: // 163 (0xa3) gt
            case IF_ICMPLE: // 164 (0xa4) le
            case GOTO: // 167 (0xa7) goto
            {
                int branchbyte1 = bytecode[pc+1] & 0xFF;
                int branchbyte2 = bytecode[pc+2] & 0xFF;
                int branchTarget =  branchbyte1 << 8 | branchbyte2 + pc; //(branchbyte1 << 8) | branchbyte2
                cout << "we got into a branch case! " << opcode << " Branch target: " << branchTarget << endl;

                basicBlocks.insert( std::pair<int, BasicBlock*>(pc, new BasicBlock(pc)) );
                basicBlocks.insert( std::pair<int, BasicBlock*>(branchTarget, new BasicBlock(branchTarget)) );

                pc+=2; // loop will add 1 to pc
                break;
            }
        }//end switch
    }// end for
}

2 个答案:

答案 0 :(得分:3)

你有这条线:

basicBlocks.insert(new BasicBlock(pc));

这是尝试将BasicBlock*插入地图,而不是一对。你的意思是:

basicBlocks.insert(std::make_pair(pc, new BasicBlock(pc)) );

答案 1 :(得分:2)

你也可以写:

basicBlocks[pc] = bb;

但是,无论使用何种方法,您都需要确保每个pc值只有一个条目。如果您需要多个条目,请使用multimap&lt;&gt;。