Using push back vs at in a vector in C

+1 vote

I'm not sure how to use vectors in C++. 

It has to do with the vector's push back technique. 

I used push back to insert entries into the vector in the first programme. 

I used at() to insert entries into the vector in the second application.

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main ()
{
  std::vector<string> myvector (3);

  cout << "In main" << endl;
  for (unsigned i=0; i<myvector.size(); i++)
  {
    myvector.push_back("hi");  //Note: using push_back here.
  }
  cout << "elements inserted into myvector" << endl;

  std::cout << "myvector contains:" << endl;
  for (auto v: myvector)
     cout << v << endl;

  // access 2nd element
  cout << "second element is " << myvector[1] << endl;

  return 0;
}

Output:   
Hangs after entering main.   
$ ./a.out   
In main

What is the problem with the way I used push back? 

This is one of the methods we use to put items into the vector, correct?

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
237 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
461 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
351 views
0 votes
0 answers

Efficient way to return a std::vector in c++

When delivering a std::vector in a function, how much data is duplicated, and how much of an optimization will it be to place the std::vector in free-store (on the heap) and provide a pointer instead, i.e. is: std::vector *f() { std::vector *result = new ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
354 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
577 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. . We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. There is, however, a syntax sugar that "copies" all fa1 from base to derived, removing all the hidden semantics: class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
567 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work.  We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base.  There is, however, a syntax sugar that "copies" all fa1 from the base to the derived. class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
581 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,420 points
2,493 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
2,296 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
268 views
0 votes
1 answer

The new syntax "= default" in C++11

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list. I'll give you an example to demonstrate the difference: #include <iostream> using namespace std; class A { public: ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
314 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP