读取字符串直到标记 (C++)
Read String Till Flag (C++)
我正在尝试读取一个字符串,直到到达“,”字符并将读取的内容存储在一个新字符串中。
例如“5,6”
// Initialise variables.
string coorPair, xCoor, yCoor
// Ask to enter coordinates.
cout << "Input coordinates: ";
// Store coordinates.
cin >> coorPair
// Break coordinates into x and y variables and convert to integers.
// ?
我还需要将 y 值存储在一个单独的变量中。
在 C++ 中执行此操作的最佳方法是什么?
此外,验证输入以转换为整数并测试值范围的最佳方法是什么?
您可以通过指定定界符并解析字符串来做到这一点
std::string delimiter = ",";
size_t pos = 0;
std::string token;
while ((pos = coorPair.find(delimiter)) != std::string::npos) {
token = coorPair.substr(0, pos);
std::cout << token << std::endl;
coorPair.erase(0, pos + delimiter.length());
}
std::cout << coorPair << endl;
{5,6} 和 {6} 中的最后一个标记示例将在 coorPair 中。
另一种方法是使用 std::getline
,如评论中所指出的:
std::string token;
while (std::getline(coorPair, token, ','))
{
std::cout << token << std::endl;
}
如果您在字符串中只有一个逗号分隔符,您可以只查找逗号在输入中第一个出现的位置和找到位置的子字符串输入。
尝试以下操作:
std::size_t pos = coorPair.find_first_of(","); //Find position of ','
xCoor = coorPair.substr(0, pos); //Substring x position string
yCoor = coorPair.substr(pos + 1); //Substring y position string
int xCoorInt = std::stoi(xCoor); //Convert x pos string to int
int yCoorInt = std::stoi(yCoor); //Convert y pos string to int
最简单的方法就是让 operator>>
为您完成所有工作:
int xCoor, yCoor;
char ch;
cout << "Input coordinates: ";
if (cin >> xCoor >> ch >> yCoor)
{
// use coordinates as needed ...
}
else
{
// bad input...
}
http://www.cplusplus.com/reference/string/string/getline/
我建议使用 getline()。
下面是我如何使用它的一个小例子。它从流中获取输入,因此您可以使用 ifstream 作为输入,或者执行我在下面所做的并将字符串转换为流。
// input data
std::string data("4,5,6,7,8,9");
// convert string "data" to a stream
std::istringstream d(data);
// output string of getline()
std::string val;
std::string x;
std::string y;
bool isX = true;
char delim = ',';
// this will read everything up to delim
while (getline(d, val, delim)) {
if (isX) {
x = val;
}
else {
y = val;
}
// alternate between assigning to X and assigning to Y
isX = !isX;
}
我正在尝试读取一个字符串,直到到达“,”字符并将读取的内容存储在一个新字符串中。
例如“5,6”
// Initialise variables.
string coorPair, xCoor, yCoor
// Ask to enter coordinates.
cout << "Input coordinates: ";
// Store coordinates.
cin >> coorPair
// Break coordinates into x and y variables and convert to integers.
// ?
我还需要将 y 值存储在一个单独的变量中。
在 C++ 中执行此操作的最佳方法是什么?
此外,验证输入以转换为整数并测试值范围的最佳方法是什么?
您可以通过指定定界符并解析字符串来做到这一点
std::string delimiter = ",";
size_t pos = 0;
std::string token;
while ((pos = coorPair.find(delimiter)) != std::string::npos) {
token = coorPair.substr(0, pos);
std::cout << token << std::endl;
coorPair.erase(0, pos + delimiter.length());
}
std::cout << coorPair << endl;
{5,6} 和 {6} 中的最后一个标记示例将在 coorPair 中。
另一种方法是使用 std::getline
,如评论中所指出的:
std::string token;
while (std::getline(coorPair, token, ','))
{
std::cout << token << std::endl;
}
如果您在字符串中只有一个逗号分隔符,您可以只查找逗号在输入中第一个出现的位置和找到位置的子字符串输入。
尝试以下操作:
std::size_t pos = coorPair.find_first_of(","); //Find position of ','
xCoor = coorPair.substr(0, pos); //Substring x position string
yCoor = coorPair.substr(pos + 1); //Substring y position string
int xCoorInt = std::stoi(xCoor); //Convert x pos string to int
int yCoorInt = std::stoi(yCoor); //Convert y pos string to int
最简单的方法就是让 operator>>
为您完成所有工作:
int xCoor, yCoor;
char ch;
cout << "Input coordinates: ";
if (cin >> xCoor >> ch >> yCoor)
{
// use coordinates as needed ...
}
else
{
// bad input...
}
http://www.cplusplus.com/reference/string/string/getline/
我建议使用 getline()。
下面是我如何使用它的一个小例子。它从流中获取输入,因此您可以使用 ifstream 作为输入,或者执行我在下面所做的并将字符串转换为流。
// input data
std::string data("4,5,6,7,8,9");
// convert string "data" to a stream
std::istringstream d(data);
// output string of getline()
std::string val;
std::string x;
std::string y;
bool isX = true;
char delim = ',';
// this will read everything up to delim
while (getline(d, val, delim)) {
if (isX) {
x = val;
}
else {
y = val;
}
// alternate between assigning to X and assigning to Y
isX = !isX;
}