๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
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

 

๋Œ“๊ธ€2