이 페이지에서

단어 수

맵-리듀스 알고리즘의 사용 방법을 보여줍니다.

Qt Concurrent 의 '단어 수 계산 ' 예제는 파일 모음에서 단어 수를 세는 문제에 맵-리듀스 알고리즘을 적용하는 방법을 보여줍니다.

먼저, 애플리케이션은 시작 경로를 선택하기 위해 QFileDialog를 시작한 다음, 콘솔에 출력을 표시합니다.

예제 실행

다음 위치에서 예제를 실행할 수 있습니다.

연산 방식 비교

텍스트 파일의 단어 수를 계산하는 단일 스레드 순차적 접근 방식과 mappedReduce()를 사용한 다중 스레드 접근 방식을 비교해 보겠습니다:

    ...
    WordCount total = singleThreadedWordCount(files);
    ...
    WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce).result();
    ...

mappedReduced 함수의 첫 번째 인수는 연산을 수행할 컨테이너입니다. 두 번째 인수는 매핑 함수 countWords() 입니다. 이 함수는 여러 스레드에 의해 병렬로 호출됩니다. 세 번째 인수는 리듀싱 함수 reduce() 입니다. 이 함수는 매핑 함수가 반환하는 각 결과에 대해 한 번씩 호출되며, 최종 계산 결과를 생성합니다.

이 함수는 WordCount 타입의 QFuture 객체를 반환합니다. 이 QFuture 에 대해 즉시 result 함수를 호출하여 결과가 준비될 때까지 후속 실행을 차단하십시오.

참고: 매핑함수는 여러 스레드에서 호출되므로 스레드 안전해야 합니다.

예제 프로젝트 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.