vectorc ++的sizeof和.size()之间的区别

Ram*_*war 1 c++ vector mpi

我正在实现MPI代码,我正在使用MPI_Bcast函数.函数的一个组件是发送的数据的数量,这里是一个被调用的向量的大小l_nMinplacesPos.我想分享这个向量的大小.我尝试了一次sizeof l_nMinplacesPos返回32,当我使用时,l_nMinplacesPos.size()我得到了2作为矢量的大小!我很困惑哪一个显示矢量的实际大小?它们两者有什么区别?

void ParaStochSimulator::broad_casting(long j){
std::cout << "i'm broad_casting" << std::endl;
l_nMinplacesPos = (*m_pcTransitionsInfo)[j]->GetManipulatedPlaces();
double val;
l_anMarking.reserve(l_nMinplacesPos.size());
for (auto lnpos : l_nMinplacesPos)
{
    val = m_anCurrentMarking[lnpos];
    l_anMarking.push_back(val);
}
for (auto marking : l_anMarking)
{
    std::cout << marking << std::endl;
}
MPI_Bcast(&l_anMarking, sizeof l_nMinplacesPos, MPI_DOUBLE, 0,   MPI_COMM_WORLD); //->here i used  l_nMinplacesPos.size() instead.
Run Code Online (Sandbox Code Playgroud)

}

void ParaStochSimulator::SimulateSingleRun()
{
//prepare a run
PrepareRun();
while ((m_nCurrentTime < m_nOutputEndPoint) && IsSimulationRunning())
    {
    deterMinTau();
    if (mnprocess_id == 0)
    {
        SimulateSingleStep1();
        std::cout << "current time:*****" << m_nCurrentTime << std::endl;
        broad_casting(m_nMinTransPos);
        std::cout << "size of mani place :" << sizeof l_nMinplacesPos << std::endl;

    }
}
PostProcessRun();
MPI_Bcast(&l_anMarking, sizeof l_nMinplacesPos, MPI_DOUBLE, 0, MPI_COMM_WORLD); //->here i used l_nMinplacesPos.size() instead.
std::cout << "size of mani place :" << sizeof l_nMinplacesPos << std::endl;


}
Run Code Online (Sandbox Code Playgroud)

Mar*_*som 7

.size()返回向量中的元素数.这就是你应该使用的.

sizeof为您提供对象定义使用的字节数,不包括通过使用指针分配的任何其他存储.它是编译器在编译时根据vector类的声明生成的静态常量.