簡體   English   中英

使用 cin 跳過預期的字符,如 scanf()

[英]Skipping expected characters like scanf() with cin

如何實現scanf("%d # %d",&a,&b); 在 C++ 中使用cin的效果如何?

您可以通過將#提取為一個字符來跳過它:

std::istringstream iss("10 # 20");

int main()
{
   int a, b; char hash;
   iss >> a >> hash >> b;

   assert(a == 10 && b == 20);
}

您可以創建自己的流操縱器。 這很容易。

#include <ios>
#include <iostream>
using namespace std;

// skips the number of characters equal to the length of given text
// does not check whether the skipped characters are the same as it
struct skip
{
    const char * text;
    skip(const char * text) : text(text) {}
};

std::istream & operator >> (std::istream & stream, const skip & x)
{
    ios_base::fmtflags f = stream.flags();
    stream >> noskipws;

    char c;
    const char * text = x.text;
    while (stream && *text++)
        stream >> c;

    stream.flags(f);
    return stream;
}

int main()
{
    int a, b;
    cin >> a >> skip(" # ") >> b;
    cout << a << ", " << b << endl;
    return 0;
}

不幸的是, istream類中沒有直接的函數來模擬它。 您可能可以使用某些函數來操作流並獲得正確的輸入,但我不熟悉它們的工作原理,因此無法告訴您如何操作。

我個人如何做到這一點的最佳建議是使用getline()將輸入放入一個字符串中,然后從那里我會做一些檢查以查看它是否與格式匹配。 所以在你的情況下,我會抓住第一個子串直到第一個空格,確保它是一個有效的小數,檢查以確保井號('#')在正確的位置,然后獲取結束數字以確保它是有效的。 如果這三個對象中的任何一個不正確,我會將一些布爾變量設置為false以踢出或返回或指示輸入無效且格式不正確的東西。

偽代碼:

...

getline(cin,myStr);

while(!formatCheck(myStr))
{
    cout<<"Not valid format for input";
    getline(cin,myStr);
}

...

bool formatCheck(string str)
{
    string firstPart=str.subString(0,firstSpaceLocation);
    string middle=str[firstSpaceLocation+1];
    string lastPart=str.subString(firstSpaceLocation+3,end);

    if(first part not a valid number || middle!="#" || last part not a valid number)
    {
        return false;
    }

    return true;
}

這是另一種方式。 您可以通過語言環境中的std::ctype<char>方面將#分類為空白字符:

#include <iostream>
#include <sstream>
#include <vector>

namespace detail
{
    enum options { add, remove };

    class ctype : public std::ctype<char>
    {
    private:
        static mask* get_table(const std::string& ws, options opt)
        {
            static std::vector<mask> table(classic_table(),
                                           classic_table() + table_size);
            for (char c : ws)
            {
                if (opt == add)
                    table[c] |= space;
                else if (opt == remove)
                    table[c] &= ~space;
            }
            return &table[0];
        }
    public:
        ctype(const std::string& ws, options opt)
            : std::ctype<char>(get_table(ws, opt)) { }
    };
}

class adjustws_impl
{
public:
    adjustws_impl(const std::string& ws, detail::options opt) :
        m_ws(ws),
        m_opt(opt)
    { }

    friend std::istream& operator>>(std::istream& is,
                                    const adjustws_impl& manip)
    {
        is.imbue(std::locale(is.getloc(),
                             new detail::ctype(manip.m_ws, manip.m_opt)));
        return is;
    }
private:
    std::string m_ws;
    detail::options m_opt;
};

adjustws_impl setws(const std::string& ws)
{
    return adjustws_impl(ws, detail::add);
}

adjustws_impl unsetws(const std::string& ws)
{
    return adjustws_impl(ws, detail::remove);
}

int main()
{
    std::istringstream iss("10 # 20");
    int a, b;

    iss >> setws("#");
    iss >> a >> b;

    iss >> unsetws("#");
    std::cout << a << ' ' << b; // 10 20
} 

您可以使用 std::istream::ignore 跳過#或任何單個字符

std::istringstream sstr("1024 # 768");

int main()
{
   int a, b;
   sstr >> a; 
   sstr.ignore(256,'#');   // ignore until hash character
   sstr >> b;
   std::cout << "a: " << a << " b: " << b << std::endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM