๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
C++

[C++] ๋ฒกํ„ฐ ์š”์†Œ์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ accumulate

by arirang_ 2024. 12. 23.

๐Ÿ“Œ import ํ—ค๋”ํŒŒ์ผ

#include <numeric> // accumulate๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”

 

<numeric>๋Š” C++ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ—ค๋” ํŒŒ์ผ ์ค‘ ํ•˜๋‚˜๋กœ,  ๋ฒกํ„ฐ๋‚˜ ๋ฐฐ์—ด ๋“ฑ์˜ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ๋ˆ„์  ํ•ฉ, ๋‚ด์ , ์ฐจ๋ถ„ ๊ณ„์‚ฐ ๋“ฑ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์šฉํ•œ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. 

 

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๋ฒกํ„ฐ๋‚˜ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ๋น ๋ฅด๊ณ  ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ˆ„์ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“Œ ์ธ์ž ์„ค๋ช…

โœ… ์ฒซ๋ฒˆ์งธ ์ธ์ž : ์‹œ์ž‘ ๋ฐ˜๋ณต์ž(๋ฒ”์œ„์˜ ์‹œ์ž‘)

โœ… ๋‘๋ฒˆ์งธ ์ธ์ž : ๋ ๋ฐ˜๋ณต์ž(๋ฒ”์œ„์˜ ๋)

โœ… ์„ธ๋ฒˆ์งธ ์ธ์ž: ๋ˆ„์  ์—ฐ์‚ฐ์˜ ์ดˆ๊ธฐ๊ฐ’

 

๐Ÿ“Œ ์˜ˆ์‹œ

โœ… ๊ธฐ๋ณธ์ ์ธ ๋ฒกํ„ฐ ์š”์†Œ์˜ ํ•ฉ

#include <numeric> // accumulate๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”
#include <vector>
#include <iostream>
using namespace std;

vector<int> vec = {1, 2, 3, 4, 5};

int main() {
   
    int sum = accumulate(vec.begin(), vec.end(), 0); // 0์€ ์ดˆ๊ธฐ๊ฐ’
    
    cout << "๋ฒกํ„ฐ ์š”์†Œ์˜ ํ•ฉ: " << sum << "\n";
    
    return 0;
}

 

โœ… ์ดˆ๊ธฐ๊ฐ’์„ ํ™œ์šฉํ•œ ๋ˆ„์ 

๐Ÿ‘‰๐Ÿป ์ดˆ๊ธฐ๊ฐ’ 100์œผ๋กœ ์„ค์ • ์‹œ

#include <numeric>
#include <vector>
#include <iostream>
using namespace std;

vector<int> numbers = {1, 2, 3, 4};

int main() {
  
    int sum_with_initial = accumulate(numbers.begin(), numbers.end(), 100);
    
    cout << "์ดˆ๊ธฐ๊ฐ’ 100์„ ํฌํ•จํ•œ ํ•ฉ: " << sum_with_initial << "\n"; // ์ถœ๋ ฅ: 110
    
    return 0;
}

 

โœ… ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋กœ ๊ณฑ, ์ฐจ, ์กฐ๊ฑด๋ถ€ ๋“ฑ์˜ ๊ณ„์‚ฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

accumulate์˜ ๋„ค๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉ์ž ์ •์˜ ์—ฐ์‚ฐ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

#include <numeric>
#include <vector>
#include <iostream>
using namespace std;

vector<int> numbers = {1, 2, 3, 4};

int main() {
    int product = accumulate(numbers.begin(), numbers.end(), 1, [](int a, int b) {
        return a * b;
    });
    
    cout << "๋ฒกํ„ฐ ์š”์†Œ์˜ ๊ณฑ: " << product << "\n"; // ์ถœ๋ ฅ: 24


    return 0;
}
#include <numeric>
#include <vector>
#include <iostream>
using namespace std;

//์ดˆ๊ธฐ๊ฐ’ 100์œผ๋กœ ์žก๊ณ  ๋นผ๊ธฐ

vector<int> numbers = {1, 2, 3, 4};

int main() {
    int product = accumulate(numbers.begin(), numbers.end(), 100, [](int a, int b) {
        return a - b;
    });
    
    cout << "๋ฒกํ„ฐ ์š”์†Œ์˜ ์ฐจ: " << product << "\n"; // ์ถœ๋ ฅ: 24


    return 0;
}

//๋ฒกํ„ฐ ์š”์†Œ์˜ ์ฐจ: 90
#include <numeric>
#include <vector>
#include <iostream>
using namespace std;

vector<int> numbers = {1, 2, 3, 4, 5, 6};

int main() {
    int even_sum = std::accumulate(numbers.begin(), numbers.end(), 0, [](int a, int b) {
        return b % 2 == 0 ? a + b : a;
    });

    cout << "์ง์ˆ˜์˜ ํ•ฉ: " << even_sum << "\n"; // ์ถœ๋ ฅ: 12
    
    return 0;
}

 

โœ… ์ˆซ์ž๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

#include <numeric>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

vector<string> words = {"Hello", " ", "World", "!"};

int main() {
    
    string sentence = accumulate(words.begin(), words.end(), string(""));
    cout << "๋ฌธ์žฅ: " << sentence << "\n"; // ์ถœ๋ ฅ: Hello World!
    return 0;
}