0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

逆引き言語差確認メモ(C++,C#,Python,Go)

Last updated at Posted at 2022-04-26

アレってこの言語でどう書くんだっけ?

別言語を使う作業に移行した時に、たまにある ”アレって、この言語でどう書くんだっけ?” をカバー

宣言

2次元配列
func.cpp
//宣言,初期化.
int table[2][3] = { {0,1,2},{3,4,5} };

//宣言,初期化(STL利用).
std::vector<std::vector<int>> table_stl = { {0,1,2},{3,4,5} };

//引数
void function(int table[2][3])
{
    ...
}
void function_stl(std::vector<std::vector<int>> table_stl) {
    ...
}

func.cs
//宣言,初期化
int[,] table = new int[,] { { 0, 1, 2 }, { 3, 4, 5 } };
//引数,返値
int[,] function(int[,] table){
    ...
    return table;
}
func.py
#宣言,初期化
table=[[0,1,2],[3,4,5]]
#引数,返値
def function(table):
    return table
func.go
//宣言,初期化
table := [][]int{{0, 1, 2}, {3, 4, 5}}

//引数,返値
func function(table [][]int) [][]int{
    return table
}

構造体
func.cpp
//定義
struct item_data{
    std::string name;
    int num;
};
//宣言,初期化
item_data data = {"名前",1};
item_data data2[3]={{"名前1",1},{"名前2",2},{"名前3",3}};
item_data* data3 = new item_data[3]{{"名前1",1},{"名前2",2},{"名前3",3}};
func.cs
//※C#では構造体は使わずclassを使ったほうが無難.
// 参照:https://techblog.kayac.com/trap-around-struct-in-csharp
 //定義
public struct item_data
{
    public string name;
    public int num;
}

//宣言,初期化
item_data data = new item_data() { name = "名前", num = 1};
item_data[] data2 ={
    new item_data{name = "名前1",num = 1},
    new item_data { name = "名前2", num = 2},
    new item_data { name = "名前3", num = 3}
};
func.py
#※Pythonには構造体がないです。代わりにdataclassを利用しています。
#  dafataclassによって、自動的に__init__()メソッドが追加されています。
#定義
import dataclasses

@dataclasses.dataclass
class item_data:
    name: str;
    num: int;

#宣言,初期化
data = item_data(u'名前',1)
data2 = [item_data(u"名前1",1),item_data(u"名前2",2),item_data(u"名前3",3)]
func.go
//定義
type item_data struct {
    name string
    num int
}
//宣言,初期化
data := item_data{"名前", 1}
data2 := []item_data{
	{name: "名前1", num: 1},
	{name: "名前2", num: 2},
	{name: "名前3", num: 3},
}

繰り返し文

forループ
func.cpp
for(int i=0;i<3;i++){
    ...
}
func.cs
for(int i=0;i<3;i++){
    ...
}
func.py
for i in range(3):
    ...

for i in range(0,3):
    ...

for i in range(0,3,1):
    ...

func.go
for i:=0; i<3; i++ {
    ...
}

foreach文
func.cpp
foreach(auto obj in list){
    ...
}
func.cs
foreach(var obj in list){
    ...
}
func.py
foreach obj in list:
    ...

func.go
//go言語にforeach分はありませんので、rangeを使います。
for _,obj := range list {
    ...
}

条件文

if文
func.cpp
if(a<b && c==d)
{
    ...
}else{
    ...
}
func.cs
if(a<b && c==d)
{
    ...
}else{
    ...
}
func.py
if (a<b) and (c==d):
    ...
else:
    ...

func.go
if (a<b) && (c==d)
{
    ...
}else{
    ...
}
switch文
func.cpp
enum itype {
  type_a,
  type_b,
  type_c
}

void function(itype c){
    switch(c)
    {
    case type_a:
        ...
        break;
    case type_b:
        ...
        break;
    case type_c:
        ...
        break;
    default:break;
    }
}

func.cs
enum itype
{
  type_a,
  type_b,
  type_c
}

void function(itype c){
    switch(c)
    {
    case itype.type_a:
        ...
        break;
    case itype.type_b:
        ...
        break;
    case itype.type_c:
        ...
        break;
    default:break;
    }
}
func.py
#PythonにはSwitch文がありませんので、if分岐になります。
from enum import Enum,auto
class itype(Enum)
    type_a=auto()
    type_b=auto()
    type_c=auto()

def function(c):
    if c==item.type_a:
        ...
    else if c==item.type_b:
        ...
    else if c==item.type_c:
        ...
    else:
        ...


func.go
//go言語にenum文がありませんので、const値で設定します。
type itype int
const (
    golang itype = iota
    type_a
    type_b
    type_c
)

func function(itype c){
    switch c {
    case type_a:
        ...
    case type_b:
        ...
    case type_c:
        ...
    default:
        ...
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?