我想在字符串中创建一个“排行榜”。这可能吗 ?我有这个代码,但它不起作用..
string leaderBoard;
leaderBoard.resize(1024);
leaderBoard.clear();
for (unsigned contor = 0; contor < myPlayer->getPlayerRoom()->playersInRoom.size(); ++contor){
leaderBoard += myPlayer->getPlayerRoom()->playersInRoom.at(contor)->getPlayerName();
leaderBoard += "-----";
leaderBoard += to_string(myPlayer->getPlayerRoom()->playersInRoom.at(contor)->getPlayerScore());
leaderBoard += '\n';
}
它放入我的leaderBoard,只有PlayerName(第一行),其他行只是不修改我的字符串。 这可以在字符串中创建“排行榜”吗?我想这样做是因为我需要通过套接字将此字符串传递给我的客户端。
PS:最终字符串应该只有3行。
答案 0 :(得分:1)
你可以使用std :: stringstream;
#include <stdio.h>
#include <stdlib.h>
#define NUM_GUESTS 200
#define FALSE 0
#define TRUE 1
#define KIDS_AGE 12
float averageAge(int ages[], int size);
int meals(int guestAges[], int size);
int search(int guestAges[], int size);
void print(int condition);
int coupleSearch(int guestAges[], int size);
void printCouples(int size, int condition2, int ageFriend);
int main(void)
{
int guestAges[NUM_GUESTS] = {42, 108, 95, 101, 90, 5, 79, 79, 83, 105, 66, 66, 2, 28, 2, 12, 116, 63, 28, 37,
112, 85, 63, 34, 53, 23, 22, 117, 39, 96, 48, 7, 12, 19, 70, 113, 108, 20, 116,
55, 24, 52, 3, 94, 34, 105, 22, 32, 54, 29, 108, 45, 23, 118, 118, 20, 84, 22,
50, 59, 77, 36, 111, 43, 49, 107, 41, 63, 65, 89, 87, 46, 51, 10, 11, 111, 7, 22,
34, 69, 70, 24, 85, 35, 37, 81, 47, 57, 12, 29, 25, 40, 27, 44, 18, 59, 39, 43,
10, 102, 34, 36, 80, 19, 25, 91, 100, 27, 114, 67, 102, 66, 45, 113, 31, 70, 18,
94, 58, 73, 107, 91, 42, 37, 36, 48, 16, 95, 72, 53, 111, 71, 22, 5, 47, 71, 28,
72, 8, 58, 98, 48, 34, 64, 66, 30, 50, 39, 102, 109, 63, 107, 27, 71, 94, 9,
61, 72, 43, 96, 11, 120, 25, 18, 69, 4, 116, 82, 3, 111, 92, 117, 15, 101, 37, 22,
109, 40, 109, 5, 2, 55, 54, 80, 19, 99, 61, 69, 8, 108, 9, 14, 49, 44, 48, 22,
31, 18, 14, 35};
int size = 0;
printf("average is: %.2f\n", averageAge(guestAges,size));
printf("Number of kids 12 and under: %d\n" , meals(guestAges, size));
print(search(guestAges,size));
coupleSearch(guestAges,size);
/*printf("Two guests with age 16 at index %d and index %d");
printf("Youngest guest age: ");
printf("Oldest guest age: ");*/
return 0;
}
/*
Function will return average age of party guests
input: age array, number of guests
output: average age
*/
float averageAge(int guestAges[], int size)
{
float sum = 0;
float avg = 0;
for(size = 0; size < NUM_GUESTS; ++size)
{
sum += guestAges[size];
}
avg = sum / NUM_GUESTS;
return avg;
}
int meals(int guestAges[], int size)
{
int kids = 0;
for (size = 0; size < NUM_GUESTS; size++)
{
if (guestAges[size] == KIDS_AGE || guestAges[size] < KIDS_AGE)
{
kids++;
}
}
return kids;
}
int search(int guestAges[], int size)
{
int condition = 0;
int age = 0;
printf("Enter age to search: ");
scanf("%d", &age);
while (size < NUM_GUESTS && age != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition = TRUE;
} else
{
condition = FALSE;
}
return condition;
}
void print(int condition)
{
if (condition == TRUE)
{
printf("Guest found!\n");
}
else if (condition == FALSE)
{
printf("No guest this age.\n");
}
}
int coupleSearch(int guestAges[], int size)
{
int condition2 = 0;
int ageFriend = 0;
printf("Enter age of guest looking for friend: ");
scanf("%d", &ageFriend);
while (size < NUM_GUESTS && ageFriend != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition2 = TRUE;
} else {
condition2 = FALSE;
}
printCouples(size,condition2,ageFriend);
return condition2;
}
void printCouples(int size, int condition2, int ageFriend)
{
if (condition2 == TRUE)
{
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size+1, size+2);
}
else if (condition2 == FALSE)
{
printf("No guest this age.\n");
}
}