Hello! :)
I'm having a problem while comparing one character from a string with a ... string.
This is a part of a pretty complex loop (or, it's complex for me, since I'm new to C++ and programming), but the case is that I need to compare the value of a given i index of a string to a string. For example:
Well, this is just a silly example program, but I hope you see whats wrong (because I don't...)
Thanks! :)
(PS: If you don't understand my english or if I have done something against the forum rules, please send me a PM, so it won't happen again.)
Nick Evan4,005
C Language: strcmp function (String Compare) In the C Programming Language, the strcmp function returns a negative, zero, or positive integer depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.
Very simple mistake:
if (str[x] 'a') should be: if (str[x] 'a') You are comparing each character from a string with another character, so you need single quotes instead of double
if I have done something against the forum rules
Actually, you are one of the few people who get the Code-tags right in their first post, so : bravo!
Edited by Nick Evan: n/a
< cpp | string | basic string
C++
Compares two character sequences.
2) Compares a
[pos1, pos1+count1) substring of this string to str. If count1 > size()- pos1 the substring is [pos1, size()) .
3) Compares a
[pos1, pos1+count1) substring of this string to a substring [pos2, pos2+count2) of str. If count1 > size()- pos1 the first substring is [pos1, size()) . Likewise, count2 > str.size()- pos2 the second substring is [pos2, str.size()) .
4) Compares this string to the null-terminated character sequence beginning at the character pointed to by s with length
Traits::length(s) .
5) Compares a
[pos1, pos1+count1) substring of this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s) If count1 > size()- pos1 the substring is [pos1, size()) .
6) Compares a
[pos1, pos1+count1) substring of this string to the characters in the range [s, s + count2) . If count1 > size()- pos1 the substring is [pos1, size()) . (Note: the characters in the range [s, s + count2) may include null characters.)
7) Implicitly converts
t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares this string to sv . This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
8) Implicitly converts
t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [pos1, pos1+count1) substring of this string to sv , as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
9) Implicitly converts
t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [pos1, pos1+count1) substring of this string to a substring [pos2, pos2+count2) of sv as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv.substr(pos2, count2)). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
![]()
A character sequence consisting of
count1 characters starting at data1 is compared to a character sequence consisting of count2 characters starting at data2 as follows. First, calculate the number of characters to compare, as if by size_type rlen =std::min(count1, count2). Then compare the sequences by calling Traits::compare(data1, data2, rlen). For standard strings this function performs character-by-character lexicographical comparison. If the result is zero (the character sequences are equal so far), then their sizes are compared as follows:
[edit]ParametersC# String Comparison
[edit]Return value
negative value if *this appears before the character sequence specified by the arguments, in lexicographical order
String Compare Dev C Download
zero if both character sequences compare equivalent
positive value if *this appears after the character sequence specified by the arguments, in lexicographical order
String Compare Dev C 4[edit]Exceptions
The overloads taking parameters named 7)noexcept specification:
pos1 or pos2 throws std::out_of_range if the argument is out of range.
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
8-9) Throws anything thrown by the conversion to
basic_string_view .
[edit]Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
String Compare C
[edit]Possible implementation[edit]NotesDev C++ Download Windows 10
For the situations when three-way comparison is not required, std::basic_string provides the usual relational operators (
< , <= , , > , etc).
By default (with the default std::char_traits), this function is not locale-sensitive. See std::collate::compare for locale-aware three-way string comparison.
[edit]Example
Output:
[edit]See also
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/compare&oldid=116896'
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |