Key ideas see LeetCode101: 15. 3Sum

class Solution {
public:
	int threeSumClosest(vector<int>& nums, int target) {
		sort(nums.begin(), nums.end());
		int closest = INT_MAX, l, r, sum, T, res;

		for (int i = 0; i < nums.size(); i++) {
			l = i + 1;
			r = nums.size() - 1;
			T = target - nums[i];
			while (l < r) {
				sum = nums[l] + nums[r];
				if (abs(sum - T) < closest) {
					res = sum + nums[i];
					closest = abs(sum - T);
				}
				if (sum == T) {
					return target;
				} if (sum < T) {
					l++;
				} else {
					r--;
				}
			}
		}
		return res;
	}
};