C++ 获取c+中两个空格之间的字符串+;
我正在从文本文件中读取坐标。例如“050100”。我将文本文件保存在字符串向量中。我想分别得到0、50和100。我想我可以把它做成两个空格之间的字符串,然后用stoi将它转换成整数。但我无法分别获得两个空格之间的字符串。我分享了我的尝试。我知道这是不对的。你能帮我找到解决办法吗 示例文本输入:Saloon 4 0 50 0 50 100 0 100。(4表示轿车有4个点。例如:4个显示(x1,y1)后的前两个整数)C++ 获取c+中两个空格之间的字符串+;,c++,string,split,whitespace,C++,String,Split,Whitespace,我正在从文本文件中读取坐标。例如“050100”。我将文本文件保存在字符串向量中。我想分别得到0、50和100。我想我可以把它做成两个空格之间的字符串,然后用stoi将它转换成整数。但我无法分别获得两个空格之间的字符串。我分享了我的尝试。我知道这是不对的。你能帮我找到解决办法吗 示例文本输入:Saloon 4 0 50 0 50 100 0 100。(4表示轿车有4个点。例如:4个显示(x1,y1)后的前两个整数) for(int j=2;j>oneValue) 值。向后推_(oneValue)
for(int j=2;j cout您可以使用std::istringstream
从文本中提取整数:
#include <sstream>
#include <vector>
#include <string>
#include <iostream>
int main()
{
std::string test = "0 50 100";
std::istringstream iss(test);
// read in values into our vector
std::vector<int> values;
int oneValue;
while (iss >> oneValue )
values.push_back(oneValue);
// output results
for(auto& v : values)
std::cout << v << "\n";
}
#包括
#包括
#包括
#包括
int main()
{
标准::字符串测试=“0 50 100”;
标准::istringstream iss(测试);
//将值读入向量
std::向量值;
int值;
而(iss>>oneValue)
值。向后推_(oneValue);
//输出结果
用于(自动和验证:值)
std::cout名称;
iss>>num;
std::cout您可以使用std::istringstream
从文本中提取整数:
#include <sstream>
#include <vector>
#include <string>
#include <iostream>
int main()
{
std::string test = "0 50 100";
std::istringstream iss(test);
// read in values into our vector
std::vector<int> values;
int oneValue;
while (iss >> oneValue )
values.push_back(oneValue);
// output results
for(auto& v : values)
std::cout << v << "\n";
}
#包括
#包括
#包括
#包括
int main()
{
标准::字符串测试=“0 50 100”;
标准::istringstream iss(测试);
//将值读入向量
std::向量值;
int值;
而(iss>>oneValue)
值。向后推_(oneValue);
//输出结果
用于(自动和验证:值)
std::cout名称;
iss>>num;
std::cout在正常情况下,解析来自输入流(如std::ifstream
)的数字很容易,因为这些流已经具有必要的解析功能
例如,要从文件输入流解析int
:
std::ifstream in{"my_file.txt"};
int number;
in >> number; // Read a token from the stream and parse it to 'int'.
假设有一个聚合类coord
,包含x和y坐标
struct coord {
int x, y;
};
您可以为类coord
添加自定义解析行为,以便在从输入流解析到类中时,它可以同时读取x和y值
std::istream& operator>>(std::istream& in, coord& c) {
return in >> c.x >> c.y; // Read two times consecutively from the stream.
}
现在,标准库中使用streams的所有工具都可以解析coord
对象。例如:
std::字符串类型;
国际nbr_coords;
std::向量坐标;
如果(在>>类型>>中){
std::copy_n(std::istream_迭代器{in},nbr_坐标,std::back_插入器(坐标));
}
这将读取正确数量的coord
对象并将其解析为一个向量,每个coord
对象同时包含一个x和一个y坐标
在正常情况下,解析输入流(如std::ifstream
)中的数字很容易,因为这些流已经具有必要的解析功能
例如,要从文件输入流解析int
:
std::ifstream in{"my_file.txt"};
int number;
in >> number; // Read a token from the stream and parse it to 'int'.
假设有一个聚合类coord
,包含x和y坐标
struct coord {
int x, y;
};
您可以为类coord
添加自定义解析行为,以便在从输入流解析到类中时,它可以同时读取x和y值
std::istream& operator>>(std::istream& in, coord& c) {
return in >> c.x >> c.y; // Read two times consecutively from the stream.
}
现在,标准库中使用streams的所有工具都可以解析coord
对象。例如:
std::字符串类型;
国际nbr_coords;
std::向量坐标;
如果(在>>类型>>中){
std::copy_n(std::istream_迭代器{in},nbr_坐标,std::back_插入器(坐标));
}
这将读取正确数量的coord
对象并将其解析为一个向量,每个coord
对象同时包含一个x和一个y坐标
拆分文本可能会有帮助。请参阅正则表达式!拆分文本可能会有帮助。请参阅正则表达式!例如“Saloon 4 0 50 0 50 100”如何应用此方法?因为Saloon不是整数,所以会出现问题。:/提取第一个字符串,然后使用循环提取整数我对sstream一无所知,它非常有用!非常感谢您的帮助!例如“Saloon 4 0 50 0 50 100”如何应用此方法?因为Saloon不是整数,所以会出现问题。:/提取第一个字符串,然后使用循环提取整数我对sstream一无所知,它非常有用!非常感谢您的帮助!