You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int a = 1;
auto a = 1;
double b = 0.0;
auto b = 0.0;
constchar c[] = "xxx"; // C string (char array)constchar* c = "xxx"; // C string (pointer)
std::string c = "xxx";
std::string c("xxx");
std::string c{"xxx"};
Variable Scopes
Python
global_var1=1deffunc(arg):
local_var=2globalglobal_var2global_var2='xxx'# globalifarg:
local_var2=0.5# scope: function
...
# if arg evaluates to True,# local_var2 is still accessible here.returnFalse
C++
int global_var1 = 1; // globalstatic std::string global_var2; // global in the current fileboolfunc(bool arg){
int local_var = 2;
global_var2 = "xxx";
if(arg){
double local_var2 = 0.5; // scope: if block
....}
// local_var2 is undefined here}
Reference
Python
s1= {"key1" : 100}
s2=s1# reference the same objects2["key2"] =200print(s1["key1"])
# {"key1":100, "key2": 200}s2= {} # s1 is NOT changed, s1 and s2# reference different objects now
C++
std::unordered_map<std::string, int> s1 = {"key1" : 100};
std::unordered_map<std::string, int> s2 = s1; // copy the whole object
std::unordered_map<std::string, int>& s2 = s1; // reference the same object
s2 = std::unordered_map<std::string, int>(); // s1 is changed, s1 and s2 still reference the same object// C++ 11auto s2 = s1; // copy the whole object (slow)auto& s2 = s1; // reference the same object// Now s2 is a reference to s1auto s3 = s2; // copy the whole s1 object. s3 is NOT a referenceconstauto& s3 = s2; // reference the same s1 object
if (a == 1 && b == 2){}
elseif(c == 3 || d == 4){}
else {}
switch(a){
case1:
...
break; // without break, will run case 2 as wellcase2: { // create a new scope if we need to define new variablesint a = 100;
break;}
default: // it's good practice to always add this
...};
Namespace & Imports
Python
# Import: File: app/bidders/ai_util.pyimportapp.bidders.ai_util# Namespace: defined by directory structure# Fully qualified names:app.bidders.ai_util.func("xxx")
C++
// Import: Files: app/bidders/ai_util.hpp & ai_util.cpp
#include"app/bidders/ai_utils.hpp"// Namespace: not related to directory structurenamespaceapp {
namespacebidder {
namespaceai_util {
voidfunc(constchar* str);
}
}
}
// Fully qualified names:app::bidders::ai_util::func("xxx");
for(int i = 0; i < 100; ++i){...}
while(cond){}
std::vector<int> a = {0, 1, 100};
for(auto& item: a){ // without &, this will copy each item
}
std::unordered_map<std::string, double> b = {{"key", 0.5}, {"key2",
1.0}};
for(auto& item: b){ // without &, this will copy each itemauto& key = item.first;
auto& val = item.second;}
obj=ObjClass()
obj.method(arg)
obj.attribute=100obj2=obj# reference the same object# Manual delete is not needed
C++
ObjClass* obj = nullptr; // prefer nullptr over NULL
ObjClass* obj = new ObjClass(); // allocate on heap
obj->method(arg);
obj->attribuge = 100;
auto obj2 = obj; // point to the same objectauto obj2 = *obj; // copy!auto& obj2 = *obj; // reference the same objectdelete obj; // when not used, manual delete is required
ObjectClass localObj(); // allocate on local stack
localObj.method(arg);
Raw pointer is not recommended. Use smart pointers
#include<memory>
std::shared_ptr<ObjClass> obj;
auto obj = std::make_shared<ObjClass>();
obj->method(arg);
obj->attribute = 100; // manual delete is not neededauto obj2 = obj; // point to the same object (no * or &)
Common Data Types (Python -> C++)
int:
int, long, unsigned int, unsigned long (size is architecture dependent)
For float, can use NAN (#include <cmath>) and use std::isnan(number) to check if it’s NAN
For string, just use empty string and use str.empty() to check if it’s empty
Define Strings
Python
s="this is a string"s2=s# s2 and s reference the same objectlen(s)
t="prefix_"+s+"_suffix"t="prefix1"+"prefix2"+ss="has\0zero"# len(s): 8
C++
#include<string>
std::string s = "this is a string";
auto s2 = s; // copy s to s2 (new object)auto& s2 = s; // s2 is a reference only
s.length();
auto t = "prefix_" + s + "_suffix"; // works but slower
std::string t = "prefix_"; t += s; t += "_suffix"; // goodauto t = "prefix1" + "prefix2" + s; // does not work
std::string s = "has\0zero"; // incorrect
s.length(): 3
std::string s("has\0zero", 8); // correctAlternative (C++ 14):
using namespace std::string_literals;
auto z = "has\0zero"s; // add "s" suffix, z is std::stringauto z = "has\0zero"; // z is char* pointer
String Methods
Python
t="test str"ift.find("sub_str") ==-1:
print("not found")
u=t[1:2] # get sub stringu=t[2:] # get sub string til endifnott:
print("empty str")
v=t.lower()
C++
#include<string>
std::string t = "test str";
if (t.find("sub_str") == std::string::npos)
std::cout << "not found\n";
if(t.empty())
std::cout << "empty str\n";
auto u = t.substr(1, 2);
auto u = t.substr(1);
#include<algorithm>
#include<cctype>std::transform(t.begin(), t.end(), t.begin(), std::tolower);
// This does not work in unicode
List (dynamic array)
Python
a= [1, 2, 3]
b= ["str1", "str2", "str3"]
c= ["xxx", {}, 100, 0.5] # cannot be done in C++a.append(100)
a.insert(2, 10)
dela[1], a[0:2]
tmp=a[0:2]
tmp=a[2]
tmp2=b[1] # reference the element
C++
#include<vector>
std::vector<int> a = {1, 2, 3};
std::vector<std::string> b = {"str1", "str2", "str3"};
std::vector<????> c // cannot be done in C++
a.push_back(100);
a.insert(a.begin() + 2, 10);
a.erase(a.begin() + 1);
a.erase(a.begin(), a.begin() + 2);
std::vector<int> tmp{a.begin(), a.begin() + 2};
auto tmp = a[2];
auto tmp2 = b[1]; // copy the element!auto& tmp2 = b[1]; // reference the element
d["new_key"] = 100;
d["no such key"] // create a new item for it
d.erase("key");
auto iter = d.find("key");
if(iter != d.end()){
// without &, this will do copyauto& e = iter->second;}
// C++ 11 ranged for loop syntaxfor(auto& item: d){ // without &, this will do copyauto& key = item.first;
auto& val = item.second;
...}