namespace std {
// operator==により、以下の演算子が使用可能になる (C++20)
template <class CharT, class Traits, class Allocator>
bool
operator!=(const basic_string<CharT, Traits, Allocator>& a,
const basic_string<CharT, Traits, Allocator>& b); // (1) C++03
template <class CharT, class Traits, class Allocator>
bool
operator!=(const basic_string<CharT, Traits, Allocator>& a,
const basic_string<CharT, Traits, Allocator>& b) noexcept; // (1) C++14
template <class CharT, class Traits, class Allocator>
constexpr bool
operator!=(const basic_string<CharT, Traits, Allocator>& a,
const basic_string<CharT, Traits, Allocator>& b) noexcept; // (1) C++20
template <class CharT, class Traits, class Allocator>
bool
operator!=(const CharT* a,
const basic_string<CharT, Traits, Allocator>& b); // (2) C++03
template <class CharT, class Traits, class Allocator>
constexpr bool
operator!=(const CharT* a,
const basic_string<CharT, Traits, Allocator>& b); // (2) C++20
template <class CharT, class Traits, class Allocator>
bool
operator!=(const basic_string<CharT, Traits, Allocator>& a,
const CharT* b); // (3) C++03
template <class CharT, class Traits, class Allocator>
constexpr bool
operator!=(const basic_string<CharT, Traits, Allocator>& a,
const CharT* b); // (3) C++20
}
概要
basic_string
オブジェクトの非等値比較を行う。
要件
- (3) パラメータ
b
が、Traits::length(b) + 1
の要素数を持つCharT
文字型の配列を指していること
戻り値
- (1)
!(a == b)
- (2)
b != a
- (3)
a.compare(b) != 0
例
#include <iostream>
#include <string>
int main()
{
std::string a = "abc";
std::string b = "abcd";
if (a != b) {
std::cout << "not equal" << std::endl;
}
else {
std::cout << "equal" << std::endl;
}
}
出力
not equal
参照
- LWG2064 - More
noexcept
issues inbasic_string
- P0980R1 Making
std::string
constexpr - P1614R2 The Mothership has Landed
- C++20での三方比較演算子の追加と、関連する演算子の自動導出