알파벳 문자가 아닌 모든 문자를 구분 기호로 간주하고 싶습니다. 어떻게해야합니까?std :: getline에 구분 기호가 아닌 것을 지정할 수 있습니까?
3
A
답변
3
수 없습니다. 기본 구분 기호 \n
입니다 : 그러므로 당신은 단지 하나의 "분할 문자"를 사용할 수 있습니다, 그러나
while (std::getline (std::cin, str, ' ') // splits at a single whitespace
은 구분 기호는 char 형이다,하지만 :
다른 구분 기호를 들어
while (std::getline (std::cin, str) // '\n' is implicit
,이를 통과 일치하지 않는 것.
입력 내용이 이미 std::string
과 같은 컨테이너 내부에있는 경우 find_first_not_of
또는 find_last_not_of
을 사용할 수 있습니다.
다른 질문으로 모든 대답을 고려 했습니까? 하나는 istream::operator>>(std::istream&, <string>)
을 사용하며 공백이 아닌 문자 시퀀스와 일치합니다.
3
그렇지 않습니다. getline
은 간단한 작업을위한 간단한 도구입니다. 좀 더 복잡한 것이 필요하다면 RegEx와 같은 좀 더 복잡한 도구를 사용해야합니다.
0
std::getline()
을 사용하여 원하는 것을 할 수는 없지만 직접 만들 수는 있습니다. 여기에 getline
변종이 있는데, 문자가 구분 기호인지 여부를 나타내는 술어 (함수, 펑터, 람다 (C++ 11 일 경우))를 지정하고 몇 가지 오버로드 문자를 사용하여 구분 기호 문자 (예 : strtok()
) :
#include <functional>
#include <iostream>
#include <string>
using namespace std;
template <typename Predicate>
istream& getline_until(istream& is, string& str, Predicate pred)
{
bool changed = false;
istream::sentry k(is,true);
if (bool(k)) {
streambuf& rdbuf(*is.rdbuf());
str.erase();
istream::traits_type::int_type ch = rdbuf.sgetc(); // get next char, but don't move stream position
for (;;ch = rdbuf.sgetc()) {
if (istream::traits_type::eof() == ch) {
is.setstate(ios_base::eofbit);
break;
}
changed = true;
rdbuf.sbumpc(); // move stream position to consume char
if (pred(istream::traits_type::to_char_type(ch))) {
break;
}
str.append(1,istream::traits_type::to_char_type(ch));
if (str.size() == str.max_size()) {
is.setstate(ios_base::failbit);
break;
}
}
if (!changed) {
is.setstate(ios_base::failbit);
}
}
return is;
}
// a couple of overloads (along with a predicate) that allow you
// to pass in a string that contains a set of delimiter characters
struct in_delim_set : unary_function<char,bool>
{
in_delim_set(char const* delim_set) : delims(delim_set) {};
in_delim_set(string const& delim_set) : delims(delim_set) {};
bool operator()(char ch) {
return (delims.find(ch) != string::npos);
};
private:
string delims;
};
istream& getline_until(istream& is, string& str, char const* delim_set)
{
return getline_until(is, str, in_delim_set(delim_set));
}
istream& getline_until(istream& is, string& str, string const& delim_set)
{
return getline_until(is, str, in_delim_set(delim_set));
}
// a simple example predicate functor
struct is_digit : unary_function<char,bool>
{
public:
bool operator()(char c) const {
return ('0' <= c) && (c <= '9');
}
};
int main(int argc, char* argv[]) {
string test;
// treat anything that's not a digit as end-of-line
while (getline_until(cin, test, not1(is_digit()))) {
cout << test << endl;
}
return 0;
}
관련 문제
- 1. 파일 구분 기호가 "/"가 아닌 플랫폼이 있습니까?
- 2. 소수 구분 기호가 항상 "."이되도록 어떻게 할 수 있습니까?
- 3. Makefile에 구분 기호가 누락되었습니다.
- 4. 기본 매개 변수로 빈 std :: vector를 지정할 수 있습니까?
- 5. DecimalFormat - 소수점 구분 기호가 없습니다.
- 6. PHP가 폭발(), 구분 기호가 반환됩니까?
- 7. Ruby의`end` 구분 기호가 중복됩니까?
- 8. UITableViewCell 및 구분 기호가 사라짐
- 9. Makefile.win : 1 : *** 구분 기호가 누락되었습니다.
- 10. Android : 메뉴의 구분 기호가 사라졌습니다.
- 11. 구분 기호가 프록시로 반환되는 이유는 무엇입니까?
- 12. 기본값을 지정할 수 있습니까?
- 13. 자바 구분 기호가 단어 건너 뛰기
- 14. 구분 기호가 다른 MySQL 다중 연결 - CONCAT_WS
- 15. 안드로이드에서 목록 항목 사이에 구분 기호가 누락되었습니다.
- 16. MultiAutoCompleteTextView에 대해 하나 이상의 구분 기호가 사용됩니까?
- 17. 사용자 지정 UITableViewCell에 줄 구분 기호가 없습니다.
- 18. 목록보기의 구분 기호가 두꺼운 경우와 두꺼운 경우
- 19. mk : 1 : *** 구분 기호가 누락되었습니다. 안드로이드 NDK
- 20. 왜 String.split에 파이프 구분 기호가 필요합니까?
- 21. 파일이 아닌 char *를 기반으로 std :: ifstream을 만들 수 있습니까?
- 22. 문자 유형이 아닌 것들과 함께 std :: basic_string을 사용할 수 있습니까?
- 23. read.table의 열 수를 지정할 수 있습니까?
- 24. std :: ofstream은 움직일 수 있습니까?
- 25. 어떻게 헤더를 지정할 수 있습니까?
- 26. TableRow 높이를 지정할 수 있습니까?
- 27. 디스플레이를 어떻게 지정할 수 있습니까?
- 28. UIColor에 태그를 지정할 수 있습니까?
- 29. unordered_map에 순서를 지정할 수 있습니까?
- 30. 컨트롤러에서 MasterPage를 지정할 수 있습니까?