整数向量

vector of integers

本文关键字:向量 整数      更新时间:2023-10-16

我想把一个向量转换成一个整数。我可以用

输出向量
vector <int> iV;
iV.push_back(3);
iV.push_back(8);
iV.push_back(6);
copy(iV.begin(),iV.end(),ostream_iterator<int>(cout,""))

但是我怎么把它变成整型呢?

编辑:使用stringstream对我正在做的事情很好。我确实想通过cout看看它到底是什么样子。我不想要精确的整型,我指的是一个整型,所以在我的例子中,它是一个等于386的整型。谢谢大家的帮助,非常感谢。

将其写入stringstream,然后像这样读取:

std::stringstream strStream;
copy(iV.begin(),iV.end(),ostream_iterator<int>(strStream,""));
int myInt;
strStream >> myInt;

我不确定这是否是你的意思,但你可以这样做:

int * ip = &iv[0];

,然后访问ip[0], ip[1]

int x;
for(vector<int>::const_iterator i = iV.begin(); i != iV.end(); i++)
    x = *i;

试试这样:

int total = 0;
for(vector<int>::const_iterator i = iV.begin(); i != iV.end(); ++i)
    total = total*10 + *i;