Jump to content
  • Advertisement
Sign in to follow this  
TommySauder

more vector trouble...

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm extremely confused. I create a vector of strings. I assign a sentance to the vector. I set the size of the vector to a variable (int). I rearrange then in nonascending order. I use v[variable].size() to get the size of the last string. My output seems to be looping around the int ( a very large number is produced ) ????

Share this post


Link to post
Share on other sites
Advertisement

#include <vector>
#include <string>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
cout << "Enter a sentace and leave the last word being exit: ";
vector<string> sentance;
string input;

string exit = "exit";

while (cin >> input) {
if (input == exit)
break;
else
sentance.push_back(input);
}

sort(sentance.begin(), sentance.end());

int longest = 0;
int shortest = 0;
int holdVectorSize = sentance.size(); // This actually holds a variable .. eg: 3

longest = sentance[holdVectorSize].size();
shortest = sentance[0].size();

cout << "Longest word in sentance: " << longest << endl; // some weird output
cout << "Shortest word in sentace: " << shortest << endl;

return 0;
}

Share this post


Link to post
Share on other sites
Array subscripts start at zero. If you have a vector with a size of 3, the subscripts are 0, 1, and 2. Not 3.

Share this post


Link to post
Share on other sites
yes, but if i put int holdVectorSize = sentance.size() - 1 the answer is wrong. (Usually ends up being the second longest word in the sentance).

This is why i'm stumped...

Share this post


Link to post
Share on other sites
Quote:
Original post by TommySauder
*** Source Snippet Removed ***

The vector is zero-based, so that you want to access element size()-1.

Of course, that code won't work like you expect, since it'll sort the strings alphabetically, not by length. If needed, you could create a comparison operator that works that way fairly easily and supply it to the sort function.

Also consider the use of front() and back(). front() returns the first element, and back() returns the last element. No worrying about array bases then.

CM

Share this post


Link to post
Share on other sites
Hold up. Why would sort() sort by the length of the string? If you want it to do that, you'll need to make your own comparison functor.

Share this post


Link to post
Share on other sites
Sort, by default, uses the less-than operator of the value type. And the less-than operator for strings compares them lexicographically (i.e. in alphabetical order).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!