C++——如何将一个字符串转换为多个整数?
C++ -- How to convert a string into multiple integers?
我在 SVG 文件中绘制形状,该文件是我通过使用 .dat 文件提供的输入生成的。
我需要从 .dat 文件中取出一行,并从中删除 4 个单独的整数,并将这些整数保存到一个向量中。为此,我试图创建一个 class 正方形,它包含 4 个整数(它们代表正方形的左上角和右下角坐标)。最好是,我可以在 class 的构造函数中执行此操作,但我不知道该怎么做。
基本上,我知道我会得到一个类似于“1 1 50 50”的字符串,我想将它变成 4 个整数。我遇到的问题是我需要将它设为 class 对象的 4 个整数,而不仅仅是 4 个整数。
class SQ
{
public:
sq() = default;
static int tl_x;
static int tl_y; //top left corner
static int br_x;
static int br_y; //bottom right corner
};
我试过下面的代码,但它显然不起作用,因为它只保存它遇到的第一个整数。
while (getline(file,s))
{
int *f = new int(stoi(s));
vec.push_back(f);
}
感谢任何帮助:)
您不必将行读入 std::string
,因为您可以一个一个地读取整数并直接将它们存储到一个向量中。试试下面的方法。
#include <fstream>
#include <vector>
int main()
{
std::ifstream ifile("./yourfile.dat");
std::vector<int> theIntegers;
for (int idx = 0, val = 0; idx < 4; ++idx)
{
ifile >> val;
theIntegers.push_back(val);
}
}
编辑:您只需通过引用构造函数传递 std::istream
即可在构造函数中读取文件。在下面的示例中,我假设您想要恰好读取四个整数,同时丢弃该行可能的剩余部分。
#include <istream>
#include <string>
#include <array>
/*
* This class holds four integers to describe a square
* On construction, it reads four integers from a given std::istream
* Note that it does not check the formatting of the input file and
* that it reads the first four integers and discards the remaining
* characters until the next newline character.
*/
class Square
{
std::array<int, 4> d_coordinates;
public:
Square() = delete;
Square(std::istream &iFile)
{
for (int idx = 0, val = 0, n = size(d_coordinates); idx < n; ++idx)
{
iFile >> val;
d_coordinates[idx] = val;
}
// Remove the newline character at the end of this line
// If you for some reason want to store multiple squares
// on a single line, then just remove the next two lines
std::string tmp;
std::getline(iFile, tmp);
}
};
int main()
{
std::ifstream ifile("./yourFile.dat");
Square x(ifile);
Square y(ifile);
Square z(ifile);
}
如果您将像 "1 1 50 50"
这样的整数行读入字符串并需要从字符串中解析整数,那么处理转换的标准 C++ 方法是从创建一个 stringstream
您的字符串并使用 iostream 从字符串流中提取整数。
例如:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
int main (void) {
std::istringstream is { "1 1 50 50\n" }; /* simulated input */
std::string s;
std::vector<int> vect;
while (getline (is, s)) { /* read string */
int f; /* declare int */
std::stringstream ss (s); /* make stringstream from s */
while ((ss >> f)) /* read ints from ss into f */
vect.push_back (f); /* add f to vector */
}
for (auto& i : vect) /* output integers in vector */
std::cout << i << '\n';
}
(如果需要单独存储所有行,只需使用std::vector<std::vector<int>> vect;
)
请注意,最初的 istringstream
只是一种模拟 getline
输入的方法。
例子Use/Output
$ ./bin/ssint
1
1
50
50
检查一下,如果您还有其他问题,请告诉我。
使用stringstream
, istream_iterator
, back_inserter
and copy
算法我们可以这样写:
while (getline(file, s))
{
auto str_stream = stringstream(s);
auto str_iter = istream_iterator< int >(str_stream);
const auto end_of_str = istream_iterator< int >();
auto back_insert_iter = back_inserter(vec);
copy(str_iter, end_of_str, back_insert_iter);
}
我在 SVG 文件中绘制形状,该文件是我通过使用 .dat 文件提供的输入生成的。
我需要从 .dat 文件中取出一行,并从中删除 4 个单独的整数,并将这些整数保存到一个向量中。为此,我试图创建一个 class 正方形,它包含 4 个整数(它们代表正方形的左上角和右下角坐标)。最好是,我可以在 class 的构造函数中执行此操作,但我不知道该怎么做。
基本上,我知道我会得到一个类似于“1 1 50 50”的字符串,我想将它变成 4 个整数。我遇到的问题是我需要将它设为 class 对象的 4 个整数,而不仅仅是 4 个整数。
class SQ
{
public:
sq() = default;
static int tl_x;
static int tl_y; //top left corner
static int br_x;
static int br_y; //bottom right corner
};
我试过下面的代码,但它显然不起作用,因为它只保存它遇到的第一个整数。
while (getline(file,s))
{
int *f = new int(stoi(s));
vec.push_back(f);
}
感谢任何帮助:)
您不必将行读入 std::string
,因为您可以一个一个地读取整数并直接将它们存储到一个向量中。试试下面的方法。
#include <fstream>
#include <vector>
int main()
{
std::ifstream ifile("./yourfile.dat");
std::vector<int> theIntegers;
for (int idx = 0, val = 0; idx < 4; ++idx)
{
ifile >> val;
theIntegers.push_back(val);
}
}
编辑:您只需通过引用构造函数传递 std::istream
即可在构造函数中读取文件。在下面的示例中,我假设您想要恰好读取四个整数,同时丢弃该行可能的剩余部分。
#include <istream>
#include <string>
#include <array>
/*
* This class holds four integers to describe a square
* On construction, it reads four integers from a given std::istream
* Note that it does not check the formatting of the input file and
* that it reads the first four integers and discards the remaining
* characters until the next newline character.
*/
class Square
{
std::array<int, 4> d_coordinates;
public:
Square() = delete;
Square(std::istream &iFile)
{
for (int idx = 0, val = 0, n = size(d_coordinates); idx < n; ++idx)
{
iFile >> val;
d_coordinates[idx] = val;
}
// Remove the newline character at the end of this line
// If you for some reason want to store multiple squares
// on a single line, then just remove the next two lines
std::string tmp;
std::getline(iFile, tmp);
}
};
int main()
{
std::ifstream ifile("./yourFile.dat");
Square x(ifile);
Square y(ifile);
Square z(ifile);
}
如果您将像 "1 1 50 50"
这样的整数行读入字符串并需要从字符串中解析整数,那么处理转换的标准 C++ 方法是从创建一个 stringstream
您的字符串并使用 iostream 从字符串流中提取整数。
例如:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
int main (void) {
std::istringstream is { "1 1 50 50\n" }; /* simulated input */
std::string s;
std::vector<int> vect;
while (getline (is, s)) { /* read string */
int f; /* declare int */
std::stringstream ss (s); /* make stringstream from s */
while ((ss >> f)) /* read ints from ss into f */
vect.push_back (f); /* add f to vector */
}
for (auto& i : vect) /* output integers in vector */
std::cout << i << '\n';
}
(如果需要单独存储所有行,只需使用std::vector<std::vector<int>> vect;
)
请注意,最初的 istringstream
只是一种模拟 getline
输入的方法。
例子Use/Output
$ ./bin/ssint
1
1
50
50
检查一下,如果您还有其他问题,请告诉我。
使用stringstream
, istream_iterator
, back_inserter
and copy
算法我们可以这样写:
while (getline(file, s))
{
auto str_stream = stringstream(s);
auto str_iter = istream_iterator< int >(str_stream);
const auto end_of_str = istream_iterator< int >();
auto back_insert_iter = back_inserter(vec);
copy(str_iter, end_of_str, back_insert_iter);
}