[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrcmp
Use std::string instead of an array of char [1,2].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrcmp
(C++) std::strcmp
A Standard function that compares two whole strings. To compare for a desired number of characters, use std::strncmp.Use std::string instead of an array of char [1,2].
int main() { const char * const word1 = "ABC"; const char * const word2 = "ABCD"; // word1 precedes word2 according to std::stdcmp assert(std::strcmp( word1,word2) < 0); // word1 is equal to word2 // according to std::stdncmp // because only the first three characters // are compared assert(std::strncmp(word1,word2,3) == 0); }
- include <cassert>
- include <cstring>
Code links
Reference
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4 Chapter 5.8.5: 'Use string rather then zero-terminated arrays of char'.
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays.'
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
