SRL Archive - C/C++ Help and Tutorials

Please, Help Me... :(


'Toxin

2012-01-26 18:30:56

So this is literally driving me up the wall, and I simply do not know what I need to do in order to do this….

Anyway, this is in relation to a programming assignment, assigned by one of my professors who likes cutting lectures more than 1/2 so we basically don't know anything and must use the internet for references.

We're asked to use vectors only.

So, I have defined a new vector of strings…



typedef std::vector<std::string> programString;


Now, we have to be able to do certain things with this vector of strings, such as adding elements to the back and FRONT of the vector. I know, I know… c++ has a library of vectors which I am using and utilizing the push_back method to add the strings to the back of the vector, however I am having a hard time with doing the same thing, except for dealing with the front of the vector.

Function for adding a string into the vector's backend :


programString push(string theString, programString z)
{
string temp = theString;
z.push_back(theString);
return z;
}

Now, how on EARTH do I do this for adding a string to the first element of the vector?

Note: The vector has an undetermined size throughout the whole program, such that the user can continuously add strings to the front or the back of it.

ShawnjohnSJ

2012-01-26 21:34:26

#include <string>
#include <vector>

int main()
{
std::vector<std::string> v;

v.push_back("Last element");

v.insert(v.begin(), "First element");

return 0;
}


Echo_

2012-01-30 11:21:16

vector.push_front()?

ShawnjohnSJ

2012-01-30 19:51:14

From: Echo_
vector.push_front()?


Vector doesn't have any such function. Vector is pretty much a resizable stack with a insert function.

Brandon

2012-01-30 20:10:06


#include <windows.h>
#include <iostream>
#include <vector>

using namespace std;

void PushFront(LPSTR NewString, std::vector<std::string> &vec)
{
vector<string>::iterator I;
I = vec.begin();
I = vec.insert(I, NewString);
}

int main()
{
vector<string> myvec;
PushFront("meh", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
PushFront("meh2", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
cin.get();
}

ShawnjohnSJ

2012-01-30 20:34:30

From: ggzz

#include <windows.h>
#include <iostream>
#include <vector>

using namespace std;

void PushFront(LPSTR NewString, std::vector<std::string> &vec)
{
vector<string>::iterator I;
I = vec.begin();
I = vec.insert(I, NewString);
}

int main()
{
vector<string> myvec;
PushFront("meh", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
PushFront("meh2", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
cin.get();
}


That code is fine, but you're using unnecessary temporaries and its not cross-platform compatible.

I revised it so that the PushFront function is a little more efficient.

#include <iostream>
#include <vector>

using namespace std;

void PushFront(const string& NewString, vector<string>& vec)
{
vec.insert(vec.begin(), NewString);
}

int main()
{
vector<string> myvec;
PushFront("meh", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
PushFront("meh2", myvec);
cout<<"First value: "<<myvec[0]<<"\n";
cin.get();
}

Brandon

2012-01-30 20:39:53

From: ShawnjohnSJ
That code is fine, but you're using unnecessary temporaries and its not cross-platform compatible.

I revised it so that the PushFront function is a little more efficient.


What? Most of the STL Library is portable.. Also I left in the iterators so he'd know where the begin and end stuff comes from.. There is no difference between what you did vs what I did.. all you did was use the iterator within the function as a parameter.. dunno where u got that portability stuff from..

Yours is just a shorter way of writing what I had..

ShawnjohnSJ

2012-01-30 21:04:55

From: ggzz
What? Most of the STL Library is portable.. Also I left in the iterators so he'd know where the begin and end stuff comes from.. There is no difference between what you did vs what I did.. all you did was use the iterator within the function as a parameter.. dunno where u got that portability stuff from..

Yours is just a shorter way of writing what I had..


First off, I got the cross-platform compatibility from you using LPSTR and including windows.h.. which is really easy to change but maybe not to a beginner at C++.

Also my version used less temporaries and more reference variables.

From: ggzz
LPSTR NewString


Using that creates a whole new variable because it has to pass-by-value. That's why I used:

From: Me
const string& NewString


Pass-by-reference won't create the extra "temporary" variable and also the const will tell the reader of the code that it won't be edited in any way.

Also you create a new iterator just to hold the value of v.begin().. which is unneeded because you can just use it directly in the function call and avoid creating the extra variable.

It might not matter using your version vs my version if you're not using the function very often, but if you want to call that function thousands of times in the fastest time possible, you want the most efficiency.

So no, the code might look the same but if you look carefully it is not. I'm not trying to throw down your code, I'm just giving you some tips on how to better it.

Brandon

2012-01-30 21:15:28

From: ShawnjohnSJ
First off, I got the cross-platform compatibility from you using LPSTR and including windows.h.. which is really easy to change but maybe not to a beginner at C++.

Also my version used less temporaries and more reference variables.



Using that creates a whole new variable because it has to pass-by-value. That's why I used:



Pass-by-reference won't create the extra "temporary" variable and also the const will tell the reader of the code that it won't be edited in any way.

Also you create a new iterator just to hold the value of v.begin().. which is unneeded because you can just use it directly in the function call and avoid creating the extra variable.

It might not matter using your version vs my version if you're not using the function very often, but if you want to call that function thousands of times in the fastest time possible, you want the most efficiency.

So no, the code might look the same but if you look carefully it is not. I'm not trying to throw down your code, I'm just giving you some tips on how to better it.


Not saying your throwing down my code.. Always open to suggestions.. The LPSTR isn't portable.. the rest definitely is.. Both just a string anyway.. The user can easily just replace that.. As for the Pass by reference rather than value, it won't make such a big difference unless your a speed freak for a couple nanoseconds being wasted. I've mentioned the reason I left the iterator in there already.

One temporary variable isn't going to hurt. Your passing a pointer.. big difference -__- Also the const won't make a difference because the value isn't going to change anyway.. your passing a constant string literal to the function not a variable..

ShawnjohnSJ

2012-01-30 21:41:13

From: ggzz
Not saying your throwing down my code.. Always open to suggestions.. The LPSTR isn't portable.. the rest definitely is.. Both just a string anyway.. The user can easily just replace that.. As for the Pass by reference rather than value, it won't make such a big difference unless your a speed freak for a couple nanoseconds being wasted. I've mentioned the reason I left the iterator in there already.

One temporary variable isn't going to hurt. Your passing a pointer.. big difference -__- Also the const won't make a difference because the value isn't going to change anyway.. your passing a constant string literal to the function not a variable..


It won't make much of a difference on todays hardware unless you're writing something like a 3D game that needs every bit of juice it can get, but if you're on slower hardware it could make a difference.

Also, I put the const in there because its a good habit to get into so your code is readable and understood by others more easily.. and it also says 'Hey.. this function won't change that variable's value at all..' sometimes its nice to know that when a function receives a reference to a variable, it won't change it.

Queso

2012-02-13 23:15:51

I remember back in the day when I got flustered with Vectors and just made my own append function that took any parameter type so long as they matched each other (e.g., int and int, int and Vector<int>, or Vector<int> and Vector<int>) and it would simply append the two. First parameter would go first in the new vector, then the second, and then it would be returned. It was beautiful.

UNFORTUNATELY ALL MY OLD WORK IS ON AN UNATTACHED HARDDRIVE :mad: