日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++17(17)-异常try catch,operator[],vector at

發布時間:2025/3/12 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++17(17)-异常try catch,operator[],vector at 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0,精通Python,62.37==>1 1,精通C++,49.21==>1 1,精通C++,49.21==>1 訪問索引超過范圍Hit any key to continue... #include <iostream> #include <cstring> #include <vector> using namespace std;class Book { private:int _id;char* _name;float _price;static inline int nowid{0};int _bookCount{1}; public : Book(int id, char* name, float price): _id(id), _price(price){int nameLen = strlen(name);_name = new char[nameLen + 1];strcpy(_name, name);}Book(char* name, float price):Book(nowid,name,price){nowid++;}~Book(){delete[] _name;}friend const Book& operator +(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount+rightBook._bookCount;return *book;}else{return leftBook;}}friend const Book& operator -(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount-rightBook._bookCount;if ( book->_bookCount<0) book->_bookCount=0;return *book;}else{return leftBook;} } Book& operator -=(const Book& otherBook){if (*this==otherBook) {_bookCount-=otherBook._bookCount;if ( _bookCount<0) _bookCount=0;return *this;}else{return *this;} } Book& operator +=(const Book& otherBook){if (*this==otherBook) {_bookCount+=otherBook._bookCount;return *this;}else{return *this;} } friend const bool operator ==(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return true;}else{return false;} } friend const bool operator !=(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return false;}else{return true;} } const void print() const{cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;}void printCount(){cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl; } }; class BookS{ private:vector<Book*> _myBooks; public:void addBook(Book* book){_myBooks.push_back(book); }void print() {for(const Book *bk:_myBooks){bk->print();}}Book& operator[](int i){ return *(_myBooks.at(i));}};int main(int argc, char **argv) {Book *book1=new Book("精通Python",62.37); Book *book2=new Book("精通C++",49.21); BookS myBks;myBks.addBook(book1); myBks.addBook(book2); myBks.print();try{ myBks[1].print();myBks[2].print(); }catch(std::out_of_range e){cout<<"訪問索引超過范圍"<<endl;}}

下面對[]的重載,對vector使用.at的方式訪問,這樣可以拋出超范圍異常,如果直接使用[]將導致超過范圍

Book& operator[](int i){ return *(_myBooks.at(i));}

總結

以上是生活随笔為你收集整理的c++17(17)-异常try catch,operator[],vector at的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。