0

我是 C++ 新手。我想在“向量”中打印一些值。

vector<GLubyte> m_coneIndices;
m_coneIndices.resize(m_bodyIndexCount + m_diskIndexCount);
vector<GLubyte>::iterator index = m_coneIndices.begin();

// Body triangles
for (int i = 0; i < coneSlices * 2; i += 2) {
    *index++ = i;
    *index++ = (i + 1) % (2 * coneSlices);
    *index++ = (i + 3) % (2 * coneSlices);
}


// Disk triangles
for (int i = 1; i < coneSlices * 2 + 1; i += 2) {
    *index++ = diskCenterIndex;
    *index++ = i;
    *index++ = (i + 2) % (2 * coneSlices);
}

这就是将值存储在“向量”中的方式。我试过了

cout << "indices"<< "\n";

for (int i=0; i<(coneSlices * 2 ); i+=2) {

    cout << m_coneIndices[i] << ' ' << m_coneIndices[i+1] << ' ' << m_coneIndices[i+2] << ' ' << "\n";
}

并打印了一些垃圾值。让我知道这段代码有什么问题。

4

2 回答 2

0

我做得对。我改变了代码

for (int i=0; i<(coneSlices * 2 ); i+=2) {

     printf("%d ",(int)m_coneIndices[i]);
}

那行得通。

于 2013-04-07T11:23:37.983 回答
0

用于push_back添加到向量中

vector<GLubyte> m_coneIndices;

// Body triangles
for (int i = 0; i < coneSlices * 3; i += 3) {
    m_coneIndices.push_back(i);
    m_coneIndices.push_back((i + 1) % (2 * coneSlices));
    m_coneIndices.push_back((i + 3) % (2 * coneSlices));
}

// Disk triangles
for (int i = 1; i < coneSlices * 2 + 1; i += 2) {
    m_coneIndices.push_back(diskCenterIndex);
    m_coneIndices.push_back(i);
    m_coneIndices.push_back((i + 2) % (2 * coneSlices));
}

打印:

cout << "indices"<< "\n";

for (size_t i = 0; i< m_coneIndices.size(); i+=3) {
    cout << m_coneIndices[i*3] << ' ' << m_coneIndices[i*3+1] << ' ' <<
            m_coneIndices[i*3+2] << ' ' << "\n";
}
于 2013-04-07T10:12:12.287 回答