KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / C++

std::isspace — witruimtetekens herkennen

std::isspace uit <cctype> herkent witruimtetekens op basis van de huidige C-locale. Zorg er bij het doorgeven van char-waarden voor dat het argument binnen het toegestane bereik valt.

Versie: C++23; repository compiler flag, production runtime not attested

Syntax

#include <cctype>
int std::isspace(int c);

std::isspace uit <cctype> geeft een waarde ongelijk aan nul terug voor witruimtetekens en anders 0; ga er niet van uit dat de functie 1 teruggeeft. In de locale "C" herkent de functie een spatie, \t, \n, \v, \f en \r.

Het argument moet gelijk zijn aan EOF of representeerbaar zijn als unsigned char; anders is het gedrag ongedefinieerd. Voor elke char-waarde is std::isspace(static_cast<unsigned char>(ch)) veilig als UCHAR_MAX <= INT_MAX (beide grenswaarden zijn beschikbaar in <climits>). Als UCHAR_MAX > INT_MAX, sla dan eerst het resultaat van static_cast<unsigned char>(ch) op in u en roep std::isspace(static_cast<int>(u)) alleen aan als u <= INT_MAX. Handel een grotere waarde apart af: de conversie naar int kan EOF opleveren in plaats van de tekenwaarde, of een andere negatieve waarde waarbij de aanroep ongedefinieerd gedrag vertoont. Controleer bij een leesresultaat dat is opgeslagen als int vóór de conversie of het gelijk is aan EOF.

De classificatie hangt af van de huidige C-locale; de functie decodeert geen UTF-8. De afzonderlijke overload std::isspace(ch, loc) uit <locale> gebruikt een expliciet doorgegeven std::locale-object en geeft bool terug.

Voorbeelden

#include <cctype>
#include <climits>
#include <clocale>
#include <iostream>

int main() {
    static_assert(UCHAR_MAX <= INT_MAX, "This example requires UCHAR_MAX <= INT_MAX.");
    std::setlocale(LC_CTYPE, "C");
    const char text[] = "A B\tC\n";
    int count = 0;
    for (char ch : text) {
        if (std::isspace(static_cast<unsigned char>(ch)) != 0) {
            ++count;
        }
    }
    std::cout << count << '\n';
}

Resultaat

3

De getoonde code en het opgegeven resultaat zijn in een geïsoleerde omgeving geverifieerd met de methode die in het verificatiebewijs voor exact deze versie is vastgelegd.

Bronnen