simple c++ examples with functions, class, gtest and etc
'L' means wchar_t, which, as opposed to a normal character, requires 16-bits of storage rather than 8-bits. Here's an example:
"A" = 41
"ABC" = 41 42 43
L"A" = 00 41
L"ABC" = 00 41 00 42 00 43- vector的特色有支持随机存取,在集合尾端增删元素很快,但是在集合中间增删元素比较费时。
#include <iostream>
using namespace std;
int main(void)
{
int a=3;
int &b=a; // 引用必须初始化
b=10;
cout<<a<<endl;
return 0;
}typedef struct
{
int x;
int y;
}Coor;
#include <iostream>
using namespace std;
int main(void)
{
Coor c1;
Coor &c=c1;
c.x=10;
c.y=20;
cout<<c1.x<<c1.y;
return 0;
}类型 *&指针引用名=指针;
#include <iostream>
using namespace std;
int main(void)
{
int a=10;
int *p=&a;
int *&q=p;
*q=20;
cout<<a<<endl;
return 0;
}void fun(int *a,int *b)
{
int c=0;
c=*a;
*a=*b;
*b=c;
}
int x=10, y=20;
fun(&x,&y);
对比
void fun(int &a, int &b)
{
int c=0;
c=a;
a=b;
b=c;
}内联函数是建议性的,由编译器决定 适用于 逻辑简单,调用频繁的函数 递归函数无法使用内联方式
inline int max(int a, int b, int c);
int main()
{
int i=10,j=20,k=30, m;
m= max(i,j,k);
cout<<"max="<<m<<endl;
return 0;
}
-> 编译的时候上面的函数会变成
int main()
{
int i=10,j=20,k=30, m;
// 注意下面的变化
int a,b,c;
// 下面的部分型党与 m=max(i,j,k);
a=i; b=j; c=k;
if(b>a) a=b;
if(c>a) a=c;
m=a;
cout<<"max="<<m<<endl;
return 0;
}内存本质是资源 -> 操作系统管理内存 -> 我们只能做 申请/归还 内存管理 = 申请/归还内存资源
// 申请内存
int *p = new int;
// 释放内存
delete p;
// 申请块内存
int *arr = new int[10];
// 释放块内存
delete []arr;c语言管理方式,配套使用,不能混搭
// 申请内存
void *malloc(size_t size);
// 释放内存
void free(void *memblock);注意事项 -> 内存分配失败
int *p=new int;
if(NULL == p)
{
// 内存分配失败
// 异常处理
}
delete p;
p = NULL;
// 处理块
int *p=new int[1000];
if(NULL == p)
{
// 内存分配失败
// 异常处理
}
delete []p;
p = NULL;
这个表示这个是类的析构函数(可以理解为反构造函数,在类对象销毁前被系统调用)。 比特一的补数 ~a 前置
- Compile something in Cygwin and you are compiling it for Cygwin.
- The purpose of Cygwin is to make porting Unix-based applications to Windows much easier
- Compile something in MinGW and you are compiling it for Windows.
- MinGW aims to simply be a Windows port of the GNU compiler tools, such as GCC, Make, Bash, and so on.
- vector的特色有支持随机存取,在集合尾端增删元素很快,但是在集合中间增删元素比较费时。
- int arr[4] ={1,2,3,4}
#include <iostream>
using namespace std;
int main(void)
{
int a=3;
int &b=a; // 引用必须初始化
b=10;
cout<<a<<endl;
return 0;
}typedef struct
{
int x;
int y;
}Coor;
#include <iostream>
using namespace std;
int main(void)
{
Coor c1;
Coor &c=c1;
c.x=10;
c.y=20;
cout<<c1.x<<c1.y;
return 0;
}类型 *&指针引用名=指针;
#include <iostream>
using namespace std;
int main(void)
{
int a=10;
int *p=&a;
int *&q=p;
*q=20;
cout<<a<<endl;
return 0;
}void fun(int *a,int *b)
{
int c=0;
c=*a;
*a=*b;
*b=c;
}
int x=10, y=20;
fun(&x,&y);
对比
void fun(int &a, int &b)
{
int c=0;
c=a;
a=b;
b=c;
}内联函数是建议性的,由编译器决定 适用于 逻辑简单,调用频繁的函数 递归函数无法使用内联方式
inline int max(int a, int b, int c);
int main()
{
int i=10,j=20,k=30, m;
m= max(i,j,k);
cout<<"max="<<m<<endl;
return 0;
}
-> 编译的时候上面的函数会变成
int main()
{
int i=10,j=20,k=30, m;
// 注意下面的变化
int a,b,c;
// 下面的部分型党与 m=max(i,j,k);
a=i; b=j; c=k;
if(b>a) a=b;
if(c>a) a=c;
m=a;
cout<<"max="<<m<<endl;
return 0;
}内存本质是资源 -> 操作系统管理内存 -> 我们只能做 申请/归还 内存管理 = 申请/归还内存资源
// 申请内存
int *p = new int;
// 释放内存
delete p;
// 申请块内存
int *arr = new int[10];
// 释放块内存
delete []arr;c语言管理方式,配套使用,不能混搭
// 申请内存
void *malloc(size_t size);
// 释放内存
void free(void *memblock);注意事项 -> 内存分配失败
int *p=new int;
if(NULL == p)
{
// 内存分配失败
// 异常处理
}
delete p;
p = NULL;
// 处理块
int *p=new int[1000];
if(NULL == p)
{
// 内存分配失败
// 异常处理
}
delete []p;
p = NULL;
这个表示这个是类的析构函数(可以理解为反构造函数,在类对象销毁前被系统调用)。 比特一的补数 ~a 前置
- Compile something in Cygwin and you are compiling it for Cygwin.
- The purpose of Cygwin is to make porting Unix-based applications to Windows much easier
- Compile something in MinGW and you are compiling it for Windows.
- MinGW aims to simply be a Windows port of the GNU compiler tools, such as GCC, Make, Bash, and so on.
- const *变量 = 固定值
-
- const 变量 = 固定变量
- const &变量 = 固定别名
- 别名必须初始化
https://stackoverflow.com/questions/6919330/return-this-in-c
myclass& myclass::MULT(int scalar){
member1_ *= scalar;
return *this;
}
myclass* myclass::MULTP(double scalar){
member2_ *= scalar;
return this;
}- 先静态编译文件
- 找到编译后的include和lib文件夹
- VC++ Dirctories 添加 Include Directories (include文件夹) 和 library directories (lib文件夹)
- Linker/Input 下的 Additional Dependencies 添加相关.lib 如 (lib文件夹下的 .lib 文件)
- C/C++|Preprocessor 下添加相关预编译变量
- 修改 Code Generation 下的 runtime library
Visual Studio(VS2017)编译并配置C/C++-libcurl开发环境
- x64 运行
- 下载解压libcurl
- 运行
buildconf.bat
- 运行
- 运行 x64 Native Tools Command Prompt for VS 2017
- cd 定位到 libcurl 下的
winbuild文件夹 如cd E:\cpp\curl-7.63.0\winbuild - 静态编译
nmake /f Makefile.vc mode=static VC=15 MACHINE=x64 DEBUG=yes- model=static 静态编译; model=dll 动态编译
- MACHINE=x64 64位; MACHINE=x86 32位;
- DEBUG=no; DEBUG=yes; -> 开启debug
- VC=15 是 VS2017
- 更详细的编译指令及说明可以打开winbuild文件夹中的
BUILD.WINDOWS.txt查看
- 查看
builds文件夹下的最短文件 如E:\cpp\curl-7.63.0\builds下的E:\cpp\curl-7.63.0\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-winssl
- cd 定位到 libcurl 下的
- 设置 VS 项目的 Properties: 右键 -> Properties
- VC++ Dirctories 里设置
- Include Directories 添加后如
E:\cpp\curl-7.63.0\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-winssl\include;$(IncludePath) - library directories 添加后如
E:\cpp\curl-7.63.0\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-winssl\lib;$(LibraryPath)
- Include Directories 添加后如
- Linker/Input 下设置
- Additional Dependencies 添加后如
libcurl_a_debug.lib;Ws2_32.lib;Wldap32.lib;winmm.lib;Crypt32.lib;Normaliz.lib;%(AdditionalDependencies)- libcurl_a.lib 或 libcurl_a.lib 是 E:\cpp\curl-7.63.0\builds\libcurl-vc15-x64-debug-static-ipv6-sspi-winssl\lib 下的lib包
- Additional Dependencies 添加后如
- C/C++|Preprocessor 下
Preprocessor Definitions添加CURL_STATICLIB- 本文使用了静态编译libcurl,所以需要将CURL_STATICLIB添加至工程
- 如 CURL_STATICLIB;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
- Debug 不用修改 Code Generation 下的 Runtime Library
- 如果没有编译debug版libcurl,则需要将“Runtime Library”改为Release版(即后面不带小写字母d)。同时官方并不建议使用“/MT”或“/MTd”。
- VC++ Dirctories 里设置
- 下载解压libcurl
- x86 运行
- 下载解压如上
- 运行 x86 Native Tools Command Prompt for VS 2017
- 静态编译 其他如上 除了 静态编译改为
nmake /f Makefile.vc mode=static VC=15 MACHINE=x86 DEBUG=yes
- 静态编译 其他如上 除了 静态编译改为
- 其他一样
测试代码
#include "pch.h"
#include <curl/curl.h>
int main(int argc, char* argv[]) {
CURL *curl = 0;
CURLcode res;
curl = curl_easy_init();
if (curl != 0) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}-
Simple C++ HTTP download using libcurl easy API – TechOverflow
-
【网络编程】httpClient抓取网页--linux C/C++ - mockmoon2011的专栏 - CSDN博客
#define CURL_STATICLIB
#include <curl/curl.h>
int main(int argc, char * argv[])
{
printf("%s\n", "main.cpp");
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (!curl)
{
printf("error\n");
return 0;
}
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com/");
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
return 0;
}
作者:研匠
链接:https://www.jianshu.com/p/518fc89abf2c
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。- C++ A Simple Web Crawler
- A very simple C++ web crawler/spider?
- Spider Examples for C++
- MelvinKool/WebCrawler
https://blog.csdn.net/xingcen/article/details/56014478
#pragma comment(lib, "libcurl_imp.lib") - Working with cURL:: Getting started the easy way (on Win32)
- What can I solve the problem "error LNK2019: unresolved external symbol"
- VC++调用libcurl的VC库使用详解 - - ITeye博客
- Option for using static version of libcurl (Visual Studio 14 2015) · Issue #196 · whoshuu/cpr
- Visual Studio 2015 curl will not statically link
CreateDirectory("./img", NULL); --> CreateDirectory(L"./img", NULL);
如 L"我的字符串" 表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节。
strlen("asd") = 3;
strlen(L"asd") = 6;
C++在字符串前加一个L作用:
如果你定义了UNICODE,那么_T宏会把字符串前面加一个L。这时 _T("ABCD") 相当于 L"ABCD" ,这是宽字符串。 如果没有定义,那么_T宏不会在字符串前面加那个L,_T("ABCD") 就等价于 "ABCD"
作者:whz_zb 来源:CSDN 原文:https://blog.csdn.net/whz_zb/article/details/7446901 版权声明:本文为博主原创文章,转载请附上博文链接!
在#include <Windows.h> 前加上 #include <WinSock2.h> https://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h
If you want to use std::string reliably, you must #include <string>.
- C++ Separate Header And Source Folders In Visual Studio 2017
- 带你玩转 Visual Studio——带你多工程开发: 一个 Solution 多个 Project
http://wiki.jikexueyuan.com/project/visual-studio/11.html
- 右键 solution -> add new project -> windows desktop | static library
- 右键 references -> add references
- 右键 project -> properties -> C/C++ 下 General Directories -> Additional Include Directories 添加相对路径如 "../Utils"