如何正确显示矢量? 我在C++中显示了向量正确的问题。
它现在打印成这样:如何正确显示矢量? 我在C++中显示了向量正确的问题。,c++,eclipse,function,vector,C++,Eclipse,Function,Vector,它现在打印成这样: 溢出器导航:A 分数:1 溢出器导航:A 分数:2 溢出器导航:A 得分:3分 溢出器导航:B 分数:1 溢出器导航:B 分数:2 … ……等等 但我希望它只打印一次“Spiller”,并多次打印“Score”,因此它将如下所示: 溢出器导航:A 分数: 1 2 三, 溢出器导航:B 分数: 1 2 三, 这是我的填充向量函数: void fyldVector(vector<Beregning>& nySpiller) { string navn
溢出器导航:A
分数:1 溢出器导航:A
分数:2 溢出器导航:A
得分:3分 溢出器导航:B
分数:1 溢出器导航:B
分数:2
…
……等等 但我希望它只打印一次“Spiller”,并多次打印“Score”,因此它将如下所示: 溢出器导航:A
分数:
1
2
三, 溢出器导航:B
分数:
1
2
三, 这是我的填充向量函数:
void fyldVector(vector<Beregning>& nySpiller) {
string navn;
int score;
cout << "Indtast antal spillere: ";
int antal;
cin >> antal;
//nySpiller.reserve( nySpiller.size() + antal );
for (int i = 0; i < antal; i++) {
cout << "Indtast spiller navn: ";
cin >> navn;
for (int j = 0; j < 3; j++) {
cout << "Indtast score: ";
cin >> score;
Beregning nyBeregning(navn, score);
nySpiller.push_back(nyBeregning);
}
}
cout << endl;
}
void fyldVector(vector和nySpiller){
字符串导航;
智力得分;
安塔尔;
//nySpiller.reserve(nySpiller.size()+antal);
for(int i=0;inavn;
对于(int j=0;j<3;j++){
cout>评分;
贝雷宁·尼贝雷宁(navn,分数);
nySpiller.向后推(Nybreigning);
}
}
cout因为您没有说明函数getScore是什么,它的返回类型是什么,所以我使用了您示例中使用的函数
void printVector( const vector<Beregning> &nySpiller )
{
string navn;
for ( const Beregning &b : nySpiller )
{
if ( navn != b.getNavn() )
{
navn = b.getNavn();
cout << "\nSpiller navn: " << navn << endl;
cout << "Score: " << endl;
}
cout << b.getScore() << endl;
}
}
void printVector(const vector和nySpiller)
{
字符串导航;
适用于(康斯特贝雷宁和b:nySpiller)
{
if(navn!=b.getNavn())
{
navn=b.getNavn();
我以前见过这个问题吗?据我记忆所及,答案是:'在循环中迭代向量,并逐个输出项'!在问题的早期版本中,nySpiller[I].getScore()
返回向量(参考)。@πάνταῥεῖ 我没有返回类型的定义。请看这里(可能从那时起已编辑):@πάνταῥεῖ 谢谢,但我不打算看那里。问题的发起者的任务是提供所有必需的信息。@πάνταῥεῖ 更重要的是我展示了这个方法。所有其他的都是细节。
void printVector( const vector<Beregning> &nySpiller )
{
string navn;
for ( const Beregning &b : nySpiller )
{
if ( navn != b.getNavn() )
{
navn = b.getNavn();
cout << "\nSpiller navn: " << navn << endl;
cout << "Score: " << endl;
}
cout << b.getScore() << endl;
}
}
void printVector( const vector<Beregning> &nySpiller )
{
string navn;
for ( vector<Beregning>::size_type i = 0; i < nySpiller.size(); i++ )
{
if ( navn != nySpiller[i].getNavn() )
{
navn = nySpiller[i].getNavn();
cout << "\nSpiller navn: " << navn << endl;
cout << "Score: " << endl;
}
cout << nySpiller[i].getScore() << endl;
}
}