考虑引号之间的争论
putting into consideration the argument between quotation marks
本文关键字:之间 更新时间:2023-10-16
我正在尝试解析命令行字符串,考虑到字符串的每个空格,引号之间都有单词。我想将 2 个引号之间的任何内容存储为 1 个索引。
vector<string> words;
stringstream ss(userInput);
string currentWord;
vector<string> startWith;
stringstream sw(userInput);
while (getline(sw, currentWord, ' '))
words.push_back(currentWord);
while (getline(ss, currentWord, '"'))
startWith.push_back(currentWord); //if(currentWord.compare("")){ continue;}
for (int i = 0; i < startWith.size(); i++)
curr
if(currentWord.compare("")){ continue;}
cout << " Index "<< i << ": " << startWith[i] << "n";
目前尚不清楚您要做什么。这是一个起点(运行它):
#include <iostream>
#include <sstream>
#include <string>
std::istream& get_word_or_quote( std::istream& is, std::string& s )
{
char c;
// skip ws and get the first character
if ( !std::ws( is ) || !is.get( c ) )
return is;
// if it is a word
if ( c != '"' )
{
is.putback( c );
return is >> s;
}
// if it is a quote (no escape sequence)
std::string q;
while ( is.get( c ) && c != '"' )
q += c;
if ( c != '"' )
throw "closing quote expected";
//
s = std::move( q );
return is;
}
int main()
{
std::istringstream is {"not-quoted "quoted" "quoted with spaces" "no closing quote!" };
try
{
std::string word;
while ( get_word_or_quote( is, word ) )
std::cout << word << std::endl;
}
catch ( const char* e )
{
std::cout << "ERROR: " << e;
}
return 0;
}
预期输出为:
not-quoted
quoted
quoted with spaces
ERROR: closing quote expected
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数