Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将整数存储到std::vector<;int>;在c++;?_C++ - Fatal编程技术网

C++ 如何将整数存储到std::vector<;int>;在c++;?

C++ 如何将整数存储到std::vector<;int>;在c++;?,c++,C++,“在C++中工作” 我有一个包含一些逗号分隔整数值的UnicodeString,例如: 卸载usCSVs=“3,4,5,6” 我需要将这些值存储在std::vector中。 实现上述功能的最佳方式是什么 问候,, JS首先解析字符串。您可以使用返回位置的std::string::find对其进行迭代。您应该找到分隔符-','。然后,获取在两个逗号之间闭合的子字符串,并使用std::stringstream。完成您可以使用std::istream\u迭代器解析字符串。在此之前,需要将所有分隔符替换

“在C++中工作”

我有一个包含一些逗号分隔整数值的UnicodeString,例如: 卸载usCSVs=“3,4,5,6”

我需要将这些值存储在std::vector中。 实现上述功能的最佳方式是什么

问候,,
JS

首先解析字符串。您可以使用返回位置的std::string::find对其进行迭代。您应该找到分隔符-','。然后,获取在两个逗号之间闭合的子字符串,并使用std::stringstream。完成

您可以使用
std::istream\u迭代器
解析字符串。在此之前,需要将所有分隔符替换为空格

 std::vector<int> myarr;
 std::string int_str = "3,4,5,6";
 std::replace( int_str.begin(), int_str.end(), ',', ' ' );
 std::stringstream in( int_str );
 std::copy( std::istream_iterator<int>(in), std::istream_iterator<int>(), std::back_inserter( myarr ) );
std::vector myarr;
std::string int_str=“3,4,5,6”;
替换(int_str.begin(),int_str.end(),',',';
std::字符串流输入(int_str);
std::copy(std::istream_迭代器(in),std::istream_迭代器(),std::back_插入器(myarr));
对于UNICODE版本,它将如下所示:

 std::wstring int_str = L"3,4,5,6";
 std::replace( int_str.begin(), int_str.end(), L',', L' ' );
 std::wstringstream in( int_str );
 std::copy( std::istream_iterator<int, wchar_t>(in), std::istream_iterator<int, wchar_t>(), std::back_inserter( myarr ) );
std::wstring int_str=L“3,4,5,6”;
替换(int_str.begin(),int_str.end(),L',,L');
std::wstringstream in(int_str);
std::copy(std::istream_迭代器(in),std::istream_迭代器(),std::back_插入器(myarr));

niiice,+1来自我,同名XD比很多,我正在寻找这样的东西。但是我对C++是新的,所以我很难理解整个逻辑。那么你能回答以下问题吗:我们真的需要用“”替换“,”吗?如果是,为什么?你能用密码详细说明最后一句话吗?你能推荐我一些好的书或网址,从那里我可以学到像你的CordBeEDES那样的字符串操作,你可以编写自己的< C++ > ISTRAMAMIORATIONER <代码>,它将与<代码>,一起工作,但是使用<代码>替换< /C>比较简单。请再告诉我一件事,复制函数中前两个参数的含义是什么,通常它表示容器的开始和结束,但在我们的例子中前两个参数是如何工作的?std::copy(std::istream_迭代器(in),std::istream_迭代器(),std::back_插入器(myarr));听起来像是我的家庭作业。。。