如把string 字符串"10,9,8,7,5",字符串转化成int分别输出,我总结了网上的方法,大概有两种方法,我写了代码并通过了运行,如下;

(1)运用c的sccanf函数输入固定格式的数据,实现分割更加快捷

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <iostream>
int main() {
	string s = "10,8,8,8,7";
	int a, b, c, d, e;
	sscanf_s(s.c_str(), "%d,%d,%d,%d,%d", &a, &b, &c, &d, &e);
	cout << a << " "<<b << " " << c << " " << d << " " << e << endl;
	system("pause");
}

(2)vector<int> 方法

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
vector<int> split(string str, string pattern)
{
	string::size_type pos;
	vector<int> result;
	str += pattern;//扩展字符串以方便操作
	int size = str.size();

	for (int i = 0; i<size; i++) {
		pos = str.find(pattern, i);
		if (pos<size) {
			int s = atoi(str.substr(i, pos - i).c_str());
			result.push_back(s);
			i = pos + pattern.size() - 1;
		}
	}
	return result;
}

int main() {
	string s = "10,8,8,8,8";
	vector<int> v = split(s, ","); //可按多个字符来分隔;
	for (vector<int>::size_type i = 0; i != v.size(); ++i) {
		cout << v[i] << " ";
	}
		
	cout<< endl;
	system("pause");
}