c++ – History of C ++. Opposite methods with the same functionality


So I wondered after a long time working with the STL Library C ++ container classes, why were opposite methods with the same functionality added to many of them? So, for example, if we consider the string class, then:

std::string::size() === std::string::length();

The methods are different, but the functionality is the same. The question is why? Is it somehow related to the history of C, or did one STL development group stupidly dislike the size() method and decided to add their own length() ? You can also give an example of all STL associative classes:

std::vector<class T>::at()  === std::vector::operator [];

Why is this? Do they differ in the speed of work? Or maybe it's a specific filling after all?


I'll start simple. The at () and operator [] methods are not the same thing at all. at () provides safe access to collection items. If the collection is out of bounds, the at () method will throw an exception, but operator [] will not. However, there is a price to pay for security with the slower operation of at ().

Scroll to Top