我是C ++的新手。我想在'vector'中打印一些值。
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);
}
这是值存储在'vector'中的方式。我试过像
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";
}
并打印了一些垃圾值。让我知道这段代码有什么问题。
答案 0 :(得分: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";
}
答案 1 :(得分:0)
我做得对。我改变了代码
for (int i=0; i<(coneSlices * 2 ); i+=2) {
printf("%d ",(int)m_coneIndices[i]);
}
并且有效。