我有输入如"(50.1003781N, 14.3925125E)".这些是纬度和经度.
我想解析这个
sscanf(string,"(%lf%c, %lf%c)",&a,&b,&c,&d);
Run Code Online (Sandbox Code Playgroud)
但是当%lf看到E数字后,它会消耗它并以指数形式将其存储为数字.有没有办法禁用它?
我怎么读长输入使用fgets(),我不太明白.
我写了这个
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buffer[10];
char *input;
while (fgets(buffer,10,stdin)){
input = malloc(strlen(buffer)*sizeof(char));
strcpy(input,buffer);
}
printf("%s [%d]",input, (int)strlen(input));
free(input);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 是*一些变量?当我这样做时,echo *它在一行上列出我的工作目录.如何强制此命令在单独的行上打印每个项目?
#include <iostream>
using namespace std;
class T
{
public:
T(int h){item = h;}
int item;
};
int main()
{
T a;
cout << a.item << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:找不到T::T(),我知道当我使用参数声明构造函数时,编译器不会生成隐式构造函数,这可以通过更改构造函数参数来修复int h = 0,但有没有其他方法可以摆脱错误?
我制作了自己的矢量模板,其中operator[]一部分:
template <class T>
T& vector<T>::operator[](unsigned int index)
{
return m_Data[index];
}
Run Code Online (Sandbox Code Playgroud)
我在类复制构造函数中使用此代码
Track::Track(const Track& src)
{
for(unsigned int i = 0; i < src.sorted.size(); i++)
AddCar(src.sorted[i]->rz, src.sorted[i]->owners.back()->name, src.sorted[i]->owners.back()->surname);
}
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误 'no match for operator[] (operands types are const vector<Track*> and unsigned int)'
我试着operator[]用const关键字重载:
const T& operator[](unsigned int);
Run Code Online (Sandbox Code Playgroud)
但这给了我一个不同的错误: const T& operator[](unsigned int); cannot be overloaded
这可能是什么原因?
我叫这个
bool cmpNAME(const PlayerCard& a, const PlayerCard& b){
return (a.Name < b.Name);//Name is std::string
}
list<PlayerCard> tmp;
//fill tmp
stable_sort(tmp.begin(), tmp.end(), cmpNAME);//error at this line
Run Code Online (Sandbox Code Playgroud)
和编译器给我错误的是它缺少运算符 - 对于PlayerCard,但为什么我为这个结构定义了比较器
错误我得到: /usr/include/c++/4.8/bits/stl_algo.h|3508|error: no match for ‘operator-’ (operand types are ‘std::_List_iterator<PlayerCard>’ and ‘std::_List_iterator<PlayerCard>’)|
我想要实现的是比较名称,如果它们是相同的,按顺序比较它我将它们推入列表
我找到了一些关于如何执行此操作的提示,但我不明白如何使用setsockopt。我有一个无限的 while 循环调用 recv,我想超时并且close(cli_socket)如果客户端在 5 秒内没有发送任何内容。
如果客户端仅发送整个预期消息的一部分,我想重置计时器并再给他 5 秒钟。
目前我有这个:
while((cut = buffer.find("\r\n")) == -1)
{
struct timeval tv;
tv.tv_sec = 5;
setsockopt(cli_socket, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
recv(cli_socket, tmpBuffer, 100, 0);
buffer += tmpBuffer;
memset(tmpBuffer, 0, 100);
}
Run Code Online (Sandbox Code Playgroud) 如何识别套接字是否已超时或没有其他数据?
我有这个:
setsockopt(cli_socket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); //5 sec timeout
while(1)
{
l = recv(socket, ...);
if(l < 0)
break;
}
Run Code Online (Sandbox Code Playgroud)
之后我检查EWOULDBLOCK或EAGAIN设置,但即使超时没有发生,他们被设置.我需要执行不同的操作,具体取决于连接时间是否成功或者我是否已成功阅读