向量 C++ 的 sizeof 和 .size() 之间的区别
The difference between sizeof and .size() for a vector c++
我正在实施 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.
}
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 向量中的元素数。这就是你应该使用的。
sizeof
为您提供对象定义使用的 字节 的数量,不包括它通过使用指针分配的任何额外存储空间。它是编译器在编译时根据 vector
class.
的声明生成的静态常量
值得一提的是 sizeof()
,当您直接处理数组单元格的内容(尤其是字符串)时,它可能会将特定单元格的长度截断为 8 个字节。在我的实际考试中,我有一个任务是创建一个 C/C++ 程序,它定位插入到数组中的每个单词的第一个字母。使用 sizeof()
后,它只返回该数组的前 8 个字符,其余的已被抛出内存,所以要小心。
我正在实施 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.
}
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 向量中的元素数。这就是你应该使用的。
sizeof
为您提供对象定义使用的 字节 的数量,不包括它通过使用指针分配的任何额外存储空间。它是编译器在编译时根据 vector
class.
值得一提的是 sizeof()
,当您直接处理数组单元格的内容(尤其是字符串)时,它可能会将特定单元格的长度截断为 8 个字节。在我的实际考试中,我有一个任务是创建一个 C/C++ 程序,它定位插入到数组中的每个单词的第一个字母。使用 sizeof()
后,它只返回该数组的前 8 个字符,其余的已被抛出内存,所以要小心。