관리자 글쓰기
문법 정리
2024. 7. 25. 14:26 - pingu-s

형 변환

#include <iostream>

using namespace std;

int main() {
  int i = 65;
  float f = 5.2f;

  // 암시적 형 변환(메모리가 큰 float으로 변환됨)
  double d = i + f;
  cout << d << endl;
  // 명시적 형 변환 double -> int
  cout << static_cast<int>(d) << endl; // 70
  // 명시적 형 변환 int -> char
  cout << static_cast<char>(i) << endl; // 'A'

  return 0;
}

 

문자열 선언 및 초기화

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str1; // 빈 문자열 선언
  string str2 = "Hello World!"; // 문자열 직접 초기화
  string str3(str2); // 문자열 복사
  string str4(str2, 0, 5); // 문자열 부분 복사 초기화 / "Hello"
  string str5(10, '*'); // 반복된 문자로 문자열 초기화 / "**********"
  
  return 0;
}

 

문자열 찾기

#include <iostream>
#include <string>

using namespace std;

int main() {
  // 문자열 초기화
  string str = "Hello, C++ World!";

  // "Hello" 문자열 찾기
  size_t pos1 = str.find("Hello");
  cout << pos1 << endl; // 0

  // 'C' 문자 찾기
  size_t pos2 = str.find('C');
  cout << pos2 << endl; // 7

  // "Hello" 문자열 팢기, 시작 인덱스 지정
  size_t start_index = 2;
  size_t pos3 = str.find("Hello", start_index);
  cout << pos3 << endl; // string::npos

  // 존재하지 않는 문자열 찾기
  size_t pos4 = str.find("Python");
  cout << pos4 << endl; // string::npos
  
  return 0;
}

 

문자열 추가, 수정

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str = "APPLE"; // 문자열 추가
  str += ", World!"; // "Apple, World!" 출력
  cout << str << endl;

  // 문자열 수정
  str[7] = 'P'; // 7번째 문자 W -> P로 수정
  cout << str << endl; // "Apple, Porld!" 출력

  str.replace(7, 4, "Col"); // 7번째 문자부터 'col'로 변경
  cout << str << endl; // "Apple, Cold" 출력
  
  return 0;
}

 

call by reference

#include <iostream>

using namespace std;

void modify(int& value) {
  value = 10; // main()의 value값 자체 변경
  cout << "주소 : " << &value << endl; // 주소 : 0x
  cout << "값 : " << value << endl; // 값 : 10
}

int main() {
  int value = 5;
  cout << "주소 : " << &value << endl; // 주소 : 0x
  cout << "값 : " << value << endl; // 값 : 5
  modify(value); // modify() 함수 호출
  cout << "값 : " << value << endl; // main() 함수 value 값 변경 / 값 : 10
  
  return 0;
}

 

auto문

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

int main() {
  auto num = 42; // int로 추론
  cout << num << endl; // 출력값 : 42

  auto pi = 3.14159; // double로 추론
  cout << pi << endl; // 출력값 : 3.14159

  auto greeting = string("Hello, World!"); // string으로 추론
  cout << greeting << endl; // 출력값 : Hello, World!
  
  return 0;
}

 

범위 기반 반복문

#include <iostream>
#include <vector>
#include <map>
#include <set>

using namespace std;

int main() {
  // vector 예
  vector<int> vec = {1, 2, 3, 4, 5};
  for (int num : vec) {
    cout << num << " ";
  }
  cout << endl;
  // 출력값 : 1 2 3 4 5

  // map 예
  map<string, int> fruitMap = {{"apple", 1}, {"banana", 2}, {"cherry", 3}};
  for (const auto& pair : fruitMap) {
    cout << pair.first << "=" << pair.second << " ";
  }
  cout << endl;
  // 출력값 : apple=1 banana=2 cherry=3

  // set 예
  set<string> fruitSet = {"apple", "banana", "cherry"};
  cout << "Set: ";
  for (const auto& fruit : fruitSet) {
    cout << fruit << " ";
  }
  cout << endl;
  // 출력값 : apple banana cherry
  
  return 0;
}

 

순방향 반복자

#include <algorithm> // find 함수를 위한 벡터
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vec = {10, 20, 30, 40, 50};

  // 순회하고 출력
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    cout << *it << " ";
  }
  cout << endl;
  // 출력값 : 10 20 30 40 50

  // 탐색
  auto result = find(vec.begin(), vec.end(), 30);
  if (result != vec.end()) {
    cout << "Found: " << *result << endl;
  } else {
    cout << "Not found" << endl;
  }
  // 출력값 : 30
  return 0;
}

 

#include <iostream>
#include <map>

using namespace std;

int main() {
  map<string, int> myMap = {{"apple", 1}, {"banana", 2}, {"cherry", 3}};

  // 순회 및 출력
  for (auto it = myMap.begin(); it != myMap.end(); ++it) {
    cout << it->first << ": "<< it->second << endl;
  }
  /*
    apple: 1
    banana: 2
    cherry: 3
  */

  // 원소 탐색
  auto result = myMap.find("banana");
  if (result != myMap.end()) {
    cout << "Found: " << result->first << " -> " << result->second << endl;
  } else {
    cout << "Not found" << endl;
  }
  /*
    Found: banana -> 2
  */
  
  return 0;
}

 

역방향 반복자

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vec = {10, 20, 30, 40, 50};

  // 순회하고 출력
  for (auto it = vec.rbegin(); it != vec.rend(); ++it){
    cout << *it << " ";
  }
  cout << endl;
  // 출력값 : 50 40 30 20 10

  // 탐색
  auto result = find(vec.rbegin(), vec.rend(), 30);
  if (result != vec.rend()) {
    cout << "Found: " << *result << endl;
  } else {
    cout << "Not found" << endl;
  }
  // 출력값 : 30
  
  return 0;
}

'개발 > C++' 카테고리의 다른 글

STL 알고리즘  (0) 2024.07.29
STL 컨테이너  (0) 2024.07.29
배열, 리스트, 벡터  (1) 2024.04.30
C++ 생성자와 정적 멤버3  (0) 2022.05.16
C++ 생성자와 정적 멤버2  (0) 2022.05.12