1// vector::back
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7 std::vector<int> myvector;
8
9 myvector.push_back(10);
10
11 while (myvector.back() != 0)
12 {
13 myvector.push_back ( myvector.back() -1 );
14 }
15
16 std::cout << "myvector contains:";
17 for (unsigned i=0; i<myvector.size() ; i++)
18 std::cout << ' ' << myvector[i];
19 std::cout << '\n';
20
21 return 0;
22}
1// my linkedin : https://www.linkedin.com/in/vaalarivan-prasanna-3a07bb203/
2vector<int> vec = {1, 2, 5, 6};
3int ele = vec.back(); //ele = 6
4//back() method does delete the last element from the vector, it just returns\n
5//the last element.
6
1#include <iostream>
2#include <vector>
3
4int main()
5{
6 std::vector<int> myvector;
7
8 //add 2 to the back
9 myvector.push_back(2);
10
11 std::cout << myvector.back() << std::endl; //this will print 2
12
13 myvector.push_back(46);
14 std::cout << myvector.back() << std::endl; //prints 46
15
16 return 0;
17
18}
19
20/*Output
212
2246
23*/
1// string::back
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str ("hello world.");
8 str.back() = '!';
9 std::cout << str << '\n';
10 return 0;
11}