본문 바로가기

책/프로그래머스

프로그래머스 - 푸드 파이트 대회

#include <string>
#include <vector>
#include <algorithm> 

using namespace std;

string solution(vector<int> food) {
    string answer = "0";
    
    string substr;
    for(int i = 1; i< food.size(); ++i)
    {
        int eachfoodNum = food[i] /2;
        if(eachfoodNum > 0)
        {
            substr.append(eachfoodNum, i + '0');
        }
    }
    answer = substr + answer;
    reverse(substr.begin(), substr.end());
    answer += substr;
    return answer;
}

string operator +

	_NODISCARD inline basic_string<_Elem, _Traits, _Alloc> operator+(
		const basic_string<_Elem, _Traits, _Alloc>& _Left,
		_In_z_ const _Elem * const _Right)
	{	// return string + NTCTS
	using _String_type = basic_string<_Elem, _Traits, _Alloc>;
	using _Size_type = typename _String_type::size_type;
	_String_type _Ans;
	_Ans.reserve(_Convert_size<_Size_type>(_Left.size() + _Traits::length(_Right)));
	_Ans += _Left;
	_Ans += _Right;
	return (_Ans);
	}

 

string append

	basic_string& append(_CRT_GUARDOVERFLOW const size_type _Count, const _Elem _Ch)
		{	// append _Count * _Ch
		auto& _My_data = this->_Get_data();
		const size_type _Old_size = _My_data._Mysize;
		if (_Count <= _My_data._Myres - _Old_size)
			{
			_My_data._Mysize = _Old_size + _Count;
			_Elem * const _Old_ptr = _My_data._Myptr();
			_Traits::assign(_Old_ptr + _Old_size, _Count, _Ch);
			_Traits::assign(_Old_ptr[_Old_size + _Count], _Elem());
			return (*this);
			}

 

 

가급적 + 쓰지 말고 append 쓰자!!