C++ 在线运行:
其他:
在C语言中,动态分配内存用 malloc()
函数,释放内存用 free()
函数
int *p = (int*) malloc( sizeof(int) * 10 ); //分配10个int型的内存空间
free(p); //释放内存
在C++中,这两个函数仍然可以使用,但是C++又新增了两个关键字,new
和 delete
:
new
用来动态分配内存delete
用来释放内存用 new
和 delete
分配内存更加简单:
int *p = new int; //分配1个int型的内存空间
delete p; //释放内存
用 new[]
分配的内存需要用 delete[]
释放,它们是一一对应的
https://www.cnblogs.com/as3lib/p/3986731.html
在windows下用<windows.h>头文件里的函数进行多字节和宽字符转换,linux下采用<iconv.h>头文件里的函数进行编码互相解析。
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#else
#include <iconv.h>
#endif
#ifdef _WIN32
std::string Utf8ToGbk(const char* src_str)
{
std::string result;
wchar_t* strSrc;
char* szRes;
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
strSrc = new wchar_t[len + 1];
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, strSrc, len);
len = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, len, NULL, NULL);
result = szRes;
if(strSrc)
delete[]strSrc;
if(szRes)
delete[]szRes;
return result;
}
std::string GbkToUtf8(const char* src_str)
{
std::string result;
wchar_t* strSrc;
char* szRes;
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
strSrc = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, src_str, -1, strSrc, len);
len = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new char[len + 1];
WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, szRes, len, NULL, NULL);
result = szRes;
if (strSrc)
delete[]strSrc;
if (szRes)
delete[]szRes;
return result;
}
#else
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
if (cd == 0) return -1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1) return -1;
iconv_close(cd);
return 0;
}
int GbkToUtf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
int Utf8ToGbk(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}
#endif
int main()
{
std::string teststr = "测试字符串";
std::cout << "原始字符串:" << teststr.c_str() << std::endl;
#ifdef _WIN32
std::cout << Utf8ToGbk(GbkToUtf8(teststr.c_str()).c_str()) << std::endl;
#else
char result_g[1024];
GbkToUtf8((char*)teststr.c_str(), strlen(teststr.c_str()), result_g, 1024);
char result_u[1024];
Utf8ToGbk(result_g, strlen(result_g), result_u, 1024);
std::cout << result_g << std::endl;
std::cout << result_u << std::endl;
#endif
return 0;
}
#include <windows.h>
#include <iostream>
using namespace std;
//读注册表
std::wstring GetRegValue(HKEY hKeyType, DWORD dwType, LPCTSTR lpPath, LPCTSTR lpName)
{
HKEY hKEY;
DWORD dataSize = MAX_PATH;
char data[MAX_PATH];
std::string strValue("");
if (RegOpenKeyEx(hKeyType, lpPath, NULL, KEY_READ, &hKEY) == ERROR_SUCCESS) //如果无法打开hKEY,则中止程序的执行
{
long lRet = RegQueryValueEx(hKEY, lpName, NULL, &dwType, (LPBYTE)data, &dataSize);
if (lRet == ERROR_SUCCESS)
{
for (int i = 0; i < (int)dataSize; i++)
{
strValue = strValue + data[i];
}
}
RegCloseKey(hKEY); // 程序结束前要关闭已经打开的 hKEY。
}
else
{
RegCreateKeyEx(hKeyType, (LPCTSTR)lpPath, 0, NULL, NULL, KEY_WRITE, NULL, &hKEY, NULL);
RegCloseKey(hKEY); // 程序结束前要关闭已经打开的 hKEY。
}
std::wstring wstrValue((wchar_t*)strValue.data(), strValue.length() / 2);
return wstrValue;
}
//写注册表
void WriteRegedit(LPCTSTR m_path, LPCTSTR m_name, wstring strParame)
{
HKEY hKEY;
if (RegOpenKeyEx(HKEY_CURRENT_USER, m_path, NULL, KEY_WRITE, &hKEY) == ERROR_SUCCESS)
{
RegSetValueEx(hKEY, m_name, NULL, REG_SZ, (BYTE*)strParame.c_str(), strParame.size() * 2);
RegCloseKey(hKEY);
}
else {
RegCreateKeyEx(HKEY_CURRENT_USER, (LPCTSTR)m_path, 0, NULL, NULL, KEY_WRITE, NULL, &hKEY, NULL);
RegSetValueEx(hKEY, m_name, NULL, REG_SZ, (BYTE*)strParame.c_str(), strParame.size() * 2);
RegCloseKey(hKEY);
}
}
int main()
{
std::wstring strValue;
strValue = GetRegValue(HKEY_CURRENT_USER, REG_SZ, L"Software\\Autodesk\\AutoCAD", L"CurVer");
std::wcout << strValue;
WriteRegedit(L"Software\\Autodesk\\AutoCAD", L"WebServiceIP", L"192.168.1.0");
Sleep(3000);
return 0;
}
CMake Error at CMakeLists.txt:4 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
原因是没有安装gcc
和g++
编译环境。安装方法很简单,一行命令搞定。参考这里:
代码在线执行:
https://codehs.com/sandbox/id/python-3-LYVkt7 python
https://codehs.com/sandbox/id/c-cO1UHt c++
C++在线运行,C++在线编译
https://www.zhihu.com/answer/2579786605
图欧学习资源库
C++ Programming Tutorial
地址:http://web.archive.org/web/20060424103805/cplus.about.com/od/beginnerctutorial/l/blcplustut.htm
Learn C++
地址:https://www.learncpp.com/
C++ Tutorial (tutorialspoint)
地址:http://www.tutorialspoint.com/cplusplus/
Modernes C++
地址:http://www.modernescpp.com/index.php/der-einstieg-in-modernes-c
CodesDope : C++ tutorial
地址:https://www.codesdope.com/cpp-introduction/
C++ Language - C++ Tutorials
地址:http://www.cplusplus.com/doc/tutorial/
C++ Tutorial (Udemy)
地址:https://www.udemy.com/course/free-learn-c-tutorial-beginners/
C++ Programming Language - GeeksforGeeks
地址:https://www.geeksforgeeks.org/c-plus-plus/
C++ Tutorial - Learn C++
地址:https://www.cprogramming.com/tutorial/c++-tutorial.html
C++ Tutotial (sololearn)
地址:https://www.sololearn.com/Course/CPlusPlus
C++ For C Programmers, Part A | Coursera
地址:https://www.coursera.org/learn/c-plus-plus-a
C++ For C Programmers, Part B | Coursera
地址:https://www.coursera.org/learn/c-plus-plus-b
C, C++ Programming Tutorials
地址:https://www.cprogramming.com/tutorial.html
C++ Tutorial (w3schools)
地址:https://www.w3schools.com/cpp/default.asp
C++ Tutorial (java2s)
地址:http://www.java2s.com/Tutorial/Cpp/CatalogCpp.htm
Learn C++ Tutorial (javapoint)
地址:https://www.javatpoint.com/cpp-tutorial
【Github】Nagi-ovo/Cherno-CPP-Notes: Cherno C++课程个人笔记
https://mp.weixin.qq.com/s?__biz=MzI1NjMxODA1Ng==&mid=2247498262&idx=1&sn=ce48c7537eb5e6b65222acd3c5a53605&scene=21#wechat_redirect
https://mp.weixin.qq.com/s?__biz=MzI1NjMxODA1Ng==&mid=2247502277&idx=1&sn=bf8bfc7b36e1b3d0eb10cddd9986f8ba&scene=21#wechat_redirect
https://mp.weixin.qq.com/s?__biz=MzI1NjMxODA1Ng==&mid=2247493694&idx=1&sn=7506a62268a44b09c355cdbe15b96204&scene=21#wechat_redirect
腾讯文档地址:https://docs.qq.com/sheet/DVHpJVmRhT3ViV09Q
金山文档地址:https://www.kdocs.cn/l/st9zs7OC086m
地址:https://pan.yuankongjian.com/
链接我放这里了:
https://docs.qq.com/sheet/DRU5MWHZCTHFGQnhM
c++ zip压缩: