C+格式的格式化输入+。像这样-->;x:y:z 我想用C++语言来输入这种格式(x:y:z)。 在输入部分,输入格式如下:
x:y:zC+格式的格式化输入+。像这样-->;x:y:z 我想用C++语言来输入这种格式(x:y:z)。 在输入部分,输入格式如下:,c++,C++,x:y:z 其中,x、y和z是三个独立的整数类型输入 您只需从这样的流中读取即可 #include <iostream> int main() { int x, y, z; char colon; if (std::cin >> x >> colon >> y >> colon >> z) { std::cout << "\nYou entered:\t" <
其中,x、y和z是三个独立的整数类型输入 您只需从这样的流中读取即可
#include <iostream>
int main() {
int x, y, z;
char colon;
if (std::cin >> x >> colon >> y >> colon >> z) {
std::cout << "\nYou entered:\t" << x << "\t" << y << "\t" << z;
}
else {
std::cerr << "\nError: Wrong input format\n";
}
return 0;
}
我不认为有人关心这个,但是,说实话
对于一般情况。您最好使用
std::getline
读取完整的输入行,然后将其拆分
对于这样的任务,你永远不需要提振
请参见拆分字符串的一些常见模式:
将字符串拆分为标记是一项非常古老的任务。有许多可用的解决方案。它们都有不同的属性。有些很难理解,有些很难开发,有些更复杂,更慢或更快或更灵活
替代品
std::strok
函数。可能不安全。也许不应该再使用了std::getline
。最常用的实现。但实际上是一种“误用”,并没有那么灵活#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <forward_list>
#include <deque>
using Container = std::vector<std::string>;
std::regex delimiter{ "," };
int main() {
// Some function to print the contents of an STL container
auto print = [](const auto& container) -> void { std::copy(container.begin(), container.end(),
std::ostream_iterator<std::decay<decltype(*container.begin())>::type>(std::cout, " ")); std::cout << '\n'; };
// Example 1: Handcrafted -------------------------------------------------------------------------
{
// Our string that we want to split
std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
Container c{};
// Search for comma, then take the part and add to the result
for (size_t i{ 0U }, startpos{ 0U }; i <= stringToSplit.size(); ++i) {
// So, if there is a comma or the end of the string
if ((stringToSplit[i] == ',') || (i == (stringToSplit.size()))) {
// Copy substring
c.push_back(stringToSplit.substr(startpos, i - startpos));
startpos = i + 1;
}
}
print(c);
}
// Example 2: Using very old strtok function ----------------------------------------------------------
{
// Our string that we want to split
std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
Container c{};
// Split string into parts in a simple for loop
#pragma warning(suppress : 4996)
for (char* token = std::strtok(const_cast<char*>(stringToSplit.data()), ","); token != nullptr; token = std::strtok(nullptr, ",")) {
c.push_back(token);
}
print(c);
}
// Example 3: Very often used std::getline with additional istringstream ------------------------------------------------
{
// Our string that we want to split
std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
Container c{};
// Put string in an std::istringstream
std::istringstream iss{ stringToSplit };
// Extract string parts in simple for loop
for (std::string part{}; std::getline(iss, part, ','); c.push_back(part))
;
print(c);
}
// Example 4: Most flexible iterator solution ------------------------------------------------
{
// Our string that we want to split
std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
Container c(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {});
//
// Everything done already with range constructor. No additional code needed.
//
print(c);
// Works also with other containers in the same way
std::forward_list<std::string> c2(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {});
print(c2);
// And works with algorithms
std::deque<std::string> c3{};
std::copy(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {}, std::back_inserter(c3));
print(c3);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用Container=std::vector;
std::regex分隔符{“,”};
int main(){
//打印STL容器内容的函数
自动打印=[](const auto&container)->void{std::copy(container.begin(),container.end(),
std::ostream_迭代器(std::cout,“”);std::cout一种可能性是,它允许指定多个分隔符,并且不需要事先了解输入的大小:
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::vector<std::string> tokens;
std::string s("x:y:z");
boost::split(tokens, s, boost::is_any_of(":"));
// "x" == tokens[0]
// "y" == tokens[1]
// "z" == tokens[2]
return 0;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
std::向量标记;
std::字符串s(“x:y:z”);
boost::split(令牌,s,boost::是(“:”)中的任意一个);
//“x”==令牌[0]
//“y”==代币[1]
//“z”==代币[2]
返回0;
}
注意:此代码将接受任何字符分隔符,甚至不接受分隔符,在后一种情况下,将吸收数字的第一个字符instead@AlanBirtles:是的,你是对的。但是请理解,谁会首先问这样的问题。我现在添加了许多代码行。但是这些代码不适合OP当前的技能因此,最初的答案是更好的。直到他们意外地输入错误,并花了很长时间调试出错误数字的原因。输入验证很重要,应该出现在每个程序中,无论多么琐碎,都可以养成良好的习惯,不引入安全漏洞
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
int main()
{
std::vector<std::string> tokens;
std::string s("x:y:z");
boost::split(tokens, s, boost::is_any_of(":"));
// "x" == tokens[0]
// "y" == tokens[1]
// "z" == tokens[2]
return 0;
}