我遇到通过TCP网络发送字符串的问题,其中字符串正在发送未输入的其他字符.
下面是我用来发送字符串的代码.
string input;
printf("Please input what you want to send...\n");
printf(">");
cin >> input;
const char* ch = (const char*)&input;
int lengthOfBytes = sizeof(input);
for (int i = 0; i < lengthOfBytes; i++)
{
n = send(socket_d, &*ch, 10, 0);
}
//Reading in characters.
if (ch == (const char*)'\r')
{
cout << "\n";
}
Run Code Online (Sandbox Code Playgroud)
这是用于接收字符串的代码.
int n;
int count = 0;
char byte;
n = recv(socket_d, &byte, 1, 0);
if (n <= 0)
{
if (WSAGetLastError() != …Run Code Online (Sandbox Code Playgroud) 我有以下两个函数,有什么方法可以使它们通用,以便它们接受任何数组,然后将其转换为字符串?
public static string PointArrayToString(Point[] array) => string.Join(" ", array);
public static string PointArrayToString(string[,] array) => string.Join(" ", array);
Run Code Online (Sandbox Code Playgroud)
例如...
public static string PointArrayToString(T[] array) => string.Join(" ", array);
Run Code Online (Sandbox Code Playgroud) 假设我有以下两个列表..
var unscoredList = new List<string> { "John", "Robert", "Rebecca" };
var scoredList = new List<WordScore>
{
new WordScore("John", 10),
new WordScore("Robert", 40),
new WordScore("Rebecca", 30)
};
Run Code Online (Sandbox Code Playgroud)
有没有办法我可以unscoredList通过scoredList首先出现得分最高的单词的值对值进行排序unscoredList?
如果需要,下面是WordScore类.
public class WordScore {
public string Word;
public int Score;
public WordScore(string word, int score) {
Word = word;
Score = score;
}
}
Run Code Online (Sandbox Code Playgroud)