我正在尝试学习c ++并尝试使用sort和qsort.sort()工作正常,但qsort没有,我不知道为什么,所以你可以帮助我,这是我试图编译的代码
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
int compvar(const void *one, const void *two)
{
int a = *((int*)one);
int b = *((int*)two);
if (a<b)
return -1;
if (a == b)
return 0;
return 1;
}
void bvect(vector<int> &vec, int num)
{
srand(time(NULL));
for(int i=0; i<num; ++i)
vec.push_back(rand()%1000 + 1);
}
void showvec(vector<int> vec)
{
for (int i=0; i<vec.size(); ++i)
cout<<vec[i]<<endl;
}
int main()
{
vector<int>numbers;
bvect(numbers, 1000);
showvec(numbers);
qsort(numbers.begin(), numbers.size(), sizeof(int), compvar);
showvec(numbers);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编译一个简单的libpcap示例,
#include<stdio.h>
#include<pcap.h>
int main(int argc, char *argv[])
{
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
char filter_exp[] = "port 23";
bpf_u_int32 mask;
bpf_u_int32 net;
dev = pcap_lookupdev(errbuf);
if (dev == NULL )
{
fprintf(stderr, "couldn't find default device: %s\n", errbuf);
return (2);
}
printf("Device: %s\n", dev);
//LOOKUP NETMASK and IP
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1)
{
fprintf(stderr, "can't get netmask for device %s\n", dev);
net = 0;
mask = 0;
}
printf("lookup\n");
pcap_t *handle;
printf("handle defined\n");
handle …Run Code Online (Sandbox Code Playgroud)