changing the delimiter for cin (c++)(更改 cin (c++) 的分隔符)
问题描述
我已重定向cin"以从文件流中读取cin.rdbug(inF.rdbug())当我使用提取运算符时,它会一直读取直到遇到一个空白字符.
I've redirected "cin" to read from a file stream cin.rdbug(inF.rdbug())
When I use the extraction operator it reads until it reaches a white space character.
是否可以使用其他分隔符?我浏览了 cplusplus.com 中的 api,但没有找到任何东西.
Is it possible to use another delimiter? I went through the api in cplusplus.com, but didn't find anything.
推荐答案
可以更改 cin 或任何其他std::istream代码>,使用std::ios_base::imbue 添加自定义 ctype facet.
如果您正在读取/etc/passwd 样式的文件,以下程序将分别读取每个 :-分隔的单词.
If you are reading a file in the style of /etc/passwd, the following program will read each :-delimited word separately.
#include <locale>
#include <iostream>
struct colon_is_space : std::ctype<char> {
  colon_is_space() : std::ctype<char>(get_table()) {}
  static mask const* get_table()
  {
    static mask rc[table_size];
    rc[':'] = std::ctype_base::space;
    rc['
'] = std::ctype_base::space;
    return &rc[0];
  }
};
int main() {
  using std::string;
  using std::cin;
  using std::locale;
  cin.imbue(locale(cin.getloc(), new colon_is_space));
  string word;
  while(cin >> word) {
    std::cout << word << "
";
  }
}
这篇关于更改 cin (c++) 的分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 cin (c++) 的分隔符
 
				
         
 
            
        基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				