C# · C++ 코드 비교
이 코스 레슨에 나오는 같은 프로그램을 두 언어로 나란히 보여 줍니다: C#, C++.
문자열과 출력
C# 문자열은 변경할 수 없는 UTF-16 참조 객체이고, C++ std::string은 변경 가능한 바이트 컨테이너입니다. C++ std::string은 변경 가능한 바이트 연속을 소유하며, 스트림 삽입은 값을 형식화합니다. 길이는 유니코드 문자가 아닌 바이트를 셉니다.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 6static void Main() {7Console.WriteLine("oak".Length);8}9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";std::cout<<name.size()<<"\n";15}문자열 동등성
C#에서 string의 ==는 텍스트를 비교하지만, 참조 동일성을 확인하는 별도의 API가 있습니다. C++의 동등성 비교도 피연산자의 자료형에 따라 달라집니다. C++의 std::string 동등성 비교는 내용을 비교합니다. 원시 포인터의 동등성 비교는 주소를 비교하며, 사용자 정의 클래스에는 의도적으로 정의한 동등성 연산이 필요합니다.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 6 static void Main() {7 object a=3, b="3"; Console.WriteLine(a.Equals(b));8 }9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="red";std::cout<<std::boolalpha<<(a==b)<<"\n";15}고정 크기 배열
C# 배열은 요소가 값 형식이더라도 참조 형식입니다. C++ std::array 자체는 값입니다. C++ std::array<T,N>는 크기가 고정된 값 형식입니다. 크기는 해당 형식의 일부이며, 일반적인 복사 작업은 요소를 복제합니다.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 6static void Main() {7int[] a={4,5,6};Console.WriteLine(a.Length+" "+a[1]);8}9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};std::cout<<a.size()<<" "<<a.at(1)<<"\n";15}값 복사
C# 구조체는 값으로 복사되는 반면, 클래스는 참조로 별칭을 만듭니다. C++ 클래스는 연산에서 허용하지 않는 경우가 아니라면 일반적으로 값으로 복사됩니다. C++의 vector와 string을 복사하면 서로 독립적인 저장 공간을 소유합니다. 원시 포인터를 복사해도 가리키는 객체를 계속 공유하며 소유권은 정의되지 않습니다.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 6 static void Main() {7 var a=new List<int>{1,2}; var b=new List<int>(a); b[0]=9; Console.WriteLine(a[0]);8 }9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<int> a{4};auto b=a;b.push_back(9);std::cout<<a.size()<<" "<<b.size()<<"\n";15}함수와 반환값
C# 메서드는 일반적으로 형식 안에 있지만, C++ 자유 함수는 이를 감싸는 클래스가 필요하지 않습니다. C++ 함수 선언은 매개변수와 반환 형식을 지정합니다. 반환값은 자동으로 출력되지 않습니다.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 static int Twice(int n) { return n*2; }6 static void Main() {7 Console.WriteLine(Twice(4));8 }9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12int triple(int n){return 3*n;}13int main(){14std::cout<<triple(4)<<"\n";15}스트림과 정확한 파싱
C#의 TryParse는 변환 성공 여부를 명시적으로 보고합니다. C++ 스트림은 추출 후 호출자가 확인해야 하는 상태를 노출합니다. C++ 스트림 추출은 스트림 상태를 통해 성공 여부를 보고합니다. 변환을 검증하고, 계약에서 입력이 정확히 일치해야 한다고 요구하는 경우 예상치 못한 후행 토큰을 거부하세요.
C#1using System;2using System.Collections.Generic;3using System.Linq;4class Program {5 6static void Main() {7int n;bool ok=int.TryParse("7",out n);Console.WriteLine(ok+" "+n);8}9}C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7");std::string word;int n;if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";15}
강의계획서
10 챕터48 강의1 프로젝트48 도전과제99 퀴즈 문항조건문과 반복문
5 강의표준 컨테이너
5 강의복사와 알고리즘
5 강의함수와 템플릿
5 강의파싱과 수명
5 강의컴포지션과 소유권
5 강의프로젝트: 도서관 데스크
4 강의최종 도전 과제
2 강의자주 묻는 질문
C# 개발자를 위한 C++은(는) 누구를 위한 코스인가요?
이미 C# 코드를 쓸 줄 알고 C++ 언어를 배우려는 분을 위한 코스입니다. 각 레슨은 익숙한 C# 코드에서 시작해 C++ 언어에서는 같은 일을 어떻게 하는지 보여 주므로, 초보자용 기초는 건너뛸 수 있습니다.
C++ 언어를 미리 알아야 하나요?
아니요. 코스는 첫 C++ 프로그램부터 시작합니다. 필요한 것은 C# 기초입니다: 변수, 조건문, 반복문, 함수, 컬렉션.
C# 개발자를 위한 C++에는 강의가 몇 개 있나요?
C# 개발자를 위한 C++에는 자기 페이스에 맞게 진행할 수 있는 48개의 인터랙티브 강의가 포함되어 있습니다.
C# 개발자를 위한 C++은(는) 무엇을 다루나요?
코스는 다음 챕터로 구성됩니다: C++ 파일과 값, 조건문과 반복문, 표준 컨테이너, 복사와 알고리즘, and more.
C# 개발자를 위한 C++은(는) 무료인가요?
네. Coddy의 모든 코스는 무료로 시작할 수 있습니다. 다운로드나 가입 없이 브라우저에서 강의를 진행할 수 있습니다.