본문 바로가기
Computer Science/기타(시스템 설계 등)

[Porting] C++와 Python 차이점 정리 (C++ vs Python)

by sohyunwriter 2021. 6. 11.

👀 C++와 Python 차이점 정리

 

C++ 코드를 Python으로 포팅하면서 두 언어의 차이에 대해 공부했다.

 

-C++ vs Python 차이점 정리

  C++ Python
모듈 가져오기 #include import
생성자(constructor) 자동으로 호출 or Class 이름으로 만들면 됨 __init__( ) method를 이용해 명시해야함
클래스 인스턴스 this self
영역 구분 방법 { } 와 ; 필요함 { } 나 ; 필요없음. 그러나 indent 꼭 해줘야함
전역변수 지칭 방법 :: global
변수 범위 제한있음 제한없음
boolean expression numeric value에 근거해 false/true return함.
0 -> false
0이 아닌 수 -> true
numeric value 말고도 다른 가능성도 존재.
none -> false
empty sequences or collections -> false
garbage collection 지원하지 않음 지원함
Naming Convention -functions, methods, classes, type names
FunctionName, 
MethodName,
ClassName, 
MyExistingClass, 
MyExistingEnum

-variables
local_var_name,
struct_var_name, 
class_var_name_

-constants, enumerator, macro
GLOBAL_CONSTANT_NAME, 
CLASS_CONSTANT_NAME,
ENUM_NAME, 
MACRO_NAME

-파일명
file_name (클래스 정의하는 파일의 경우 class_name)
-functions, methods, attributes, variables
function_name
method_name
function_parameter_name,
global_var_name
class_var_name
local_var_name,

-classes
ClassName

-constants
GLOBAL_CONSTANT_NAME
CLASS_CONSTANT_NAME

-기타
module_name
package_name
ExceptionName 
접근제한자    

 

-자료구조

귀찮아서 선언과 insert하는 방법만 정리했다. 

C++ Python
Unordered Map

#include <unordered_map>

unordered_map<string, string> mymap;
mymap = { {"one", "uno"}, {"two", "dos"} };
mymap["three"] = "tres";
Dictionary

mydict = {"one": "uno", "two": "dos"}
mydict["three"] = "tres"
Vector

#include <vector>

vector<int> myvector;
myvector.push_back(1);
List

mylist = []
mylist.append(1)
Array

int myarray[] = {2, 3, 1};
List

mylist = [2, 3, 1]
Unordered Set

#include <underorder_set>

set<int> mySet = {3, 6, 4};
Set

myset = set([2, 3, 1])
Char

char mychar = 's'  // char values use single quotes
String

mychar = "s"
String 

#include <string>

string mystring = " // C++ strings use double quotes
String

mystring = "sohyun"
Char array

char mystring[] = {"Hello World!"} // char array uses double quotes
String

mystring = "sohyun"
Stack

#include <stack>

stack<int> s;
s.push(10);
List

mylist = [10]
Queue

#include <queue>

queue<int> q;
q.push(10);
List를 사용하는 방식도 있지만 deque를 권장

from collections import deque
q = dequeue()
q.append(10)
Priority Queue

#include <queue>

//min heap
priority_queue<int, vector<int, greater<int> > myminheap;
myminheap.push(6);
myminheap.push(1);
myminheap.push(0);

//max heap
priority_queue<int> mymaxheap;
mymaxheap.push(5);
mymaxheap.push(1);
mymaxheap.push(10);
heapq

import heapq

#min heap
myminheap = [6, 1, 0]
heapq.heapify(myminheap)

#max heap
mymaxheap = [5, 1, 10]
mymaxheap = [-item for item in mymaxheap]
heapq.heapify(mymaxheap)
Linked List  
Tree  
Graph  

 

 

💡 C++ Reference

https://www.cplusplus.com/reference/

https://en.cppreference.com/w/

 

 

💡 Python Reference

https://docs.python.org/3/reference/

 

python naming convention

 

-참고문헌

더보기

https://runestone.academy/runestone/books/published/cpp4python/index.html

: 진짜 레전드 사이트. 이건 정말 갓 사이트다!!

 

Porting a C++ Application to Python

 

Porting a C++ Application to Python — Qt for Python

Porting a C++ Application to Python Qt for Python lets you use Qt APIs in a Python application. So the next question is: What does it take to port an existing C++ application? Try porting a Qt C++ application to Python to understand this. Before you start,

doc.qt.io

https://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names

 

What is the naming convention in Python for variable and function names?

Coming from a C# background the naming convention for variables and method names are usually either camelCase or PascalCase: // C# example string thisIsMyVariable = "a" public void ThisIsMyMethod(...

stackoverflow.com

https://www.educba.com/python-vs-c-plus-plus/

 

Python vs C++ | Find Out The 9 Essential Differences

Guide to Python vs C++.Here we have discussed Python and C++ head to head comparison, key differences along with infographics and comparison table.

www.educba.com

https://conservative-vector.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC%EA%B3%BC-C-%EC%B0%A8%EC%9D%B4%EC%A0%90-%EC%A0%95%EB%A6%AC

 

파이썬과 C++ 차이점 정리

파이썬을 공부하면서 C++과 다른점들을 메모해두고 있다. 마지막 업데이트 : 2020.05.03 (댓글로 알려주신 내용 추가) 0. 기본 문법 C++은 int a, int b이렇게 해야하지만 파이썬은 그냥 a, b이렇게 쓴다.

conservative-vector.tistory.com

https://stackoverflow.com/questions/3106689/pointers-in-python

 

Pointers in Python?

I know Python doesn't have pointers, but is there a way to have this yield 2 instead >>> a = 1 >>> b = a # modify this line somehow so that b "points to" a >>> a = 2 >...

stackoverflow.com

https://jimmy-shen.medium.com/priority-queue-in-c-and-python-80e8cb39cc4b

 

Priority queue in C++ and python

By default, python support min priority queue while C++ supports max priority queue.

jimmy-shen.medium.com

https://www.daleseo.com/python-queue/

 

파이썬에서 큐(queue) 자료 구조 사용하기

Engineering Blog by Dale Seo

www.daleseo.com