프로그래밍/C++

list

swedu 2023. 2. 27. 12:22
728x90
반응형

2023.02.27  최초 작성

 

 

노드와 노드가 연결되어있는 연결리스트 구조이다.

연속적인 메모리 공간이 아니므로 인덱스로 임의 접근이 불가하다.

탐색 속도가 벡터에 비해 느리다.

 
 

선언 및 초기화

list<char> char_list{ 'A', 'B', 'C' }; 정해진 요소로 초기화
list<char> char_list2(char_list); 다른 목록을 복사 생성자로 초기화
list<char> char_list3(10, 'a'); 정해진 요소로 일정 크기만큼 초기화
list<char> char_list4; 빈 리스트 초기화

 

 

반복자

begin() beginning iterator를 반환
end() end iterator를 반환

 

 

요소 접근

*iterator iterator가 가리키는 원소에 접근
front() 첫번째 원소를 반환
back() 마지막 원소를 반환

 

요소 삽입과 삭제

push_front(element) 리스트 제일 앞에 원소 추가
pop_front() 리스트 제일 앞에 원소 삭제
push_back(element) 리스트 제일 뒤에 원소 추가
pop_back() 리스트 제일 뒤에 원소 삭제
insert(iterator, element) iterator가 가리키는 부분 앞에 원소를 추가
erase(iterator) iterator가 가리키는 부분에 원소를 삭제
emplace_back(element) 리스트의 마지막 부분에 새로운 요소 추가(복사생성자 X)

 

 

용량

empty() 비었으면 true 아니면 false
size() 리스트의 크기 반환
max_size() 리스트가 system에서 만들어 질 수 있는 최대 크기 반환

 

#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
    list<int> list01{10, 20, 30, 40, 50 };
    for (int n : list01) {
        cout << n << endl;
    }

    cout << "리스트의 첫 번째 요소 : " << list01.front() << endl;
    for (list<int>::iterator begin_iter = list01.begin(); begin_iter != list01.end(); ++begin_iter) {
        cout << *begin_iter << endl;
    }
    cout << "리스트의 마지막 요소 : " << list01.back() << endl;

    list01.push_front(100);
    list01.push_back(200);
    list<int>::iterator iter = list01.begin();
    iter++;
    iter++;
    list01.insert(iter, 25); // 삽입
    iter++;
    iter++;
    list01.erase(iter);  // 삭제
    for (int element : list01) {
        cout << element << endl;
    }
    cout << "리스트 원소 수 : " << list01.size() << endl;
    while (!list01.empty()) {
        cout << list01.front() << endl;
        list01.pop_front();
    }
    cout << "리스트 원소 수 : " << list01.size() << endl;
	return EXIT_SUCCESS;
}

[실행 결과]

10
20
30
40
50
리스트의 첫 번째 요소 : 10
10
20
30
40
50
리스트의 마지막 요소 : 50
100
10
25
20
30
50
200
리스트 원소 수 : 7
100
10
25
20
30
50
200
리스트 원소 수 : 0

 

 

#include <iostream>
#include <list>
#include <string>
using namespace std;
class Student {
private:
	string name;
	int kor;
	int eng;
	int math;
public:
	Student(string name, int kor, int eng, int math) :
		name(name), kor(kor), eng(eng), math(math) {}
	~Student() {}
	void print() {
		cout << "학생이름 : " << this->name << endl;
		cout << "국어점수 : " << this->kor << endl;
		cout << "영어점수 : " << this->eng << endl;
		cout << "수학점수 : " << this->math << endl;
	}
};
int main() {
	list<Student> list01;
	list01.emplace_back("홍길동", 100, 90, 80);
	list01.emplace_back("이순신", 80, 90, 100);
	list01.emplace_back("유관순", 77, 88, 99);
	cout << "학생 수 : " << list01.size() << endl;
	while (!list01.empty()) {
		list01.back().print();
		list01.pop_back();
	}
	cout << endl;
	list<Student*> list02;
	list02.push_back(new Student("김구", 99, 88, 88));
	list02.push_back(new Student("안중근", 100, 99, 98));
	cout << "학생 수 : " << list02.size() << endl;
	for (Student* s : list02) {
		s->print();
	}
	cout << endl << "iterator로 접근 : " << endl;
	list<Student*>::iterator iter = list02.begin();
	iter++;
	(*iter)->print();
    return EXIT_SUCCESS;
}

[실행 결과]

학생 수 : 3
학생이름 : 유관순
국어점수 : 77
영어점수 : 88
수학점수 : 99
학생이름 : 이순신
국어점수 : 80
영어점수 : 90
수학점수 : 100
학생이름 : 홍길동
국어점수 : 100
영어점수 : 90
수학점수 : 80

학생 수 : 2
학생이름 : 김구
국어점수 : 99
영어점수 : 88
수학점수 : 88
학생이름 : 안중근
국어점수 : 100
영어점수 : 99
수학점수 : 98

iterator로 접근 :
학생이름 : 안중근
국어점수 : 100
영어점수 : 99
수학점수 : 98

 

 

728x90
반응형

 

728x90
반응형

'프로그래밍 > C++' 카테고리의 다른 글

파일 입출력  (0) 2023.02.28
vector  (0) 2023.02.27
가상함수, 순수가상함수, 추상클래스  (0) 2023.02.24
상속  (0) 2023.02.23
연산자 중복  (0) 2023.02.21