반응형
string 자료형
**string**은 문자열을 저장하는 자료형으로, <string> 헤더 파일을 포함하여 사용할 수 있습니다. 문자열을 다루는 다양한 기능과 멤버 함수를 제공합니다.
#include <iostream>#include <string>int main() {
std::string name = "Alice";
std::cout << "이름: " << name << std::endl;
// 문자열 길이 구하기
std::cout << "이름 길이: " << name.length() << std::endl;
// 문자열 붙이기
name += " Smith";
std::cout << "변경된 이름: " << name << std::endl;
return 0;
}
실행 결과:
이름: Alice
이름 길이: 5
변경된 이름: Alice Smith
배열 (array)
배열은 동일한 자료형의 데이터를 일렬로 나열하여 저장하는 자료구조입니다. 배열은 <array> 헤더 파일을 포함하지 않고도 사용할 수 있습니다.
cppCopy code
#include <iostream>int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// 배열 요소에 접근하기
std::cout << "첫 번째 숫자: " << numbers[0] << std::endl;
std::cout << "두 번째 숫자: " << numbers[1] << std::endl;
return 0;
}
실행 결과:
첫 번째 숫자: 1
두 번째 숫자: 2
동적 배열 (vector)
**vector**는 배열과 유사하면서도 크기가 동적으로 조정될 수 있는 자료구조입니다. <vector> 헤더 파일을 포함하여 사용할 수 있습니다.
#include <iostream>#include <vector>int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
// 요소 추가
numbers.push_back(6);
// 요소 접근
std::cout << "첫 번째 숫자: " << numbers[0] << std::endl;
std::cout << "두 번째 숫자: " << numbers[1] << std::endl;
// 요소 개수
std::cout << "총 개수: " << numbers.size() << std::endl;
return 0;
}
실행 결과:
Copy code
첫 번째 숫자: 1
두 번째 숫자: 2
총 개수: 6
- 연관 배열 (map)
**map**은 키-값 쌍으로 데이터를 저장하는 자료구조로, <map> 헤더 파일을 포함하여 사용할 수 있습니다.
#include <iostream>#include <map>int main() {
std::map<std::string, int> scores;
scores["Alice"] = 90;
scores["Bob"] = 85;
scores["Charlie"] = 95;
// 요소 접근
std::cout << "Alice의 점수: " << scores["Alice"] << std::endl;
std::cout << "Bob의 점수: " << scores["Bob"] << std::endl;
// 요소 개수
std::cout << "총 개수: " << scores.size() << std::endl;
return 0;
}
실행 결과:
Alice의 점수: 90
Bob의 점수: 85
총 개수: 3
반응형
'C++' 카테고리의 다른 글
[C++] 함수와 반환값 (0) | 2023.07.28 |
---|---|
[C++] 입출력문 cout , cin , endl (\n) 사용법 (0) | 2023.07.28 |
sort 사용법 (0) | 2020.08.22 |
String에 입력 Vector<char>에 복사하기 (0) | 2020.08.13 |
이중벡터 선언 (0) | 2020.08.12 |