简体   繁体   English

向量C ++的sizeof和.size()之间的区别

[英]The difference between sizeof and .size() for a vector c++

I'm implementing a MPI code and I'm using MPI_Bcast function. 我正在实现MPI代码,并且正在使用MPI_Bcast函数。 One of the function's components is the number of data sent which is here the size of a vector called l_nMinplacesPos . 函数的组成部分之一是发送的数据数量,在此是称为l_nMinplacesPos的向量的大小。 I want to share the size of this vector. 我想分享这个向量的大小。 I tried once sizeof l_nMinplacesPos which returned 32 and when I used l_nMinplacesPos.size() I got 2 as a size of the vector!!!. 我曾经尝试过一次sizeof l_nMinplacesPos返回32,当我使用l_nMinplacesPos.size()我得到2作为向量的大小! I'm confused which one of them displays the actual size of the vector? 我很困惑其中哪一个显示矢量的实际大小? and what is the difference between both of them? 两者之间有什么区别?

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.

} }

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;


}

.size() returns the number of elements in the vector. .size()返回向量中元素的数量。 That's what you should be using. 那就是你应该使用的。

sizeof gives you the number of bytes used by the object definition, excluding any additional storage it has allocated through the use of pointers. sizeof提供了对象定义使用的字节数,不包括它通过使用指针分配的任何其他存储。 It is a static constant generated by the compiler at compile time, based on the declaration of the vector class. 它是编译器在编译时根据vector类的声明生成的静态常量。

Worth mentioning about sizeof() is also the fact, that when you work directly on your array's cells' content (especially strings), it may cut off the length of that specific cell to 8 bytes. 值得一提的是sizeof() ,当您直接处理数组单元格的内容(尤其是字符串)时,它可能会将特定单元格的长度缩短为8个字节。 On my practical exam I had a task to create a C/C++ program, which locates the first letter of every word inserted into the array. 在我的实践考试中,我有一个创建C / C ++程序的任务,该程序将找到插入到数组中的每个单词的第一个字母。 After using sizeof() it returned only first 8 characters of that array, the rest has been thrown out of memory, so be careful. 使用sizeof()它仅返回该数组的前8个字符,其余的已被抛出内存,因此请当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM