Trying push a string backwards into a vector - c++

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class thing{
public:
vector<unsigned short> vec;
thing(string);
};
int main(){
thing adam("12345");
for(int i = adam.vec.size() - 1;i >= 0;i--){
cout << adam.vec[i] << endl;
}
}
thing::thing(string str){
for(int i = str.size() - 1; i >= 0;i--){
cout << str[i] << endl;
vec.push_back(str[i]);
}
}
I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse, but instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49. This happens when I call push_back() and I'm confused on why.

I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse
You probably don't really want to do that. As #JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?
instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49
You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:
Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.

for(int i = adam.vec.size() - 1;i >= 0;i--)
cout << adam.vec[i] << endl;
Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:
thing adam("12345");
for(size_t i = 0; i < adam.vec.size(); i++)
cout << adam.vec[i] << ' ';
cout << endl;
//or
for(const auto &element : adam.vec)
cout << element << ' ';
cout << endl;
Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:
thing::thing(string str)
{
std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
}
Or use iterators to traverse forward or backward.

Related

Vector display iterator ends prematurely [duplicate]

This question already has answers here:
sizeof() a vector
(6 answers)
Closed 2 years ago.
I'm currently working on a final project for one of my introductory classes in college, and I can't seem to find out why my output is cutting out prematurely. I'm trying to append the lines of a text file in the same directory into a vector and randomly select from the vector to get a name for an enemy in a text-based fighting game, but during testing it seems like the vector either only contains three items or the last for loop (the one before the comment blocks, I'm working on it in chunks) only displays three of the possible names. Here's the code:
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <ctype.h>
using namespace std;
int main() {
srand(time(0));
int userInput;
int player_health = 100;
int boss_health = 200;
int boss_index = rand() % 3;
ifstream boss_list;
string boss_name = "";
string boss_inputs[3] = {"MACHINE GUN", "BOMB ATTACK", "CANE SMACK"};
string user_inputs[3] = {"SWORD ATTACK", "BOMB ATTACK", "MAGIC ATTACK"};
vector<string> boss_list_holder = {};
unordered_map<string, int> attacks = {
{"SWORD ATTACK", rand() % 100},
{"BOMB ATTACK", rand() % 100},
{"MAGIC ATTACK", rand() % 100}
};
unordered_map<string, int> boss_attacks = {
{"MACHINE GUN", rand() % 75},
{"BOMB ATTACK", rand() % 80},
{"CANE SMACK", rand() % 50}
};
boss_list.open("names.txt", ios::in);
while(getline(boss_list, boss_name)) {
//cout << boss_name << endl;
boss_list_holder.push_back(boss_name);
}
for(int j=0; j<(sizeof(boss_list_holder)/sizeof(boss_list_holder[0])); j++) {
cout << boss_list_holder[j] << endl;
}
//cout << "BOSS FIGHT SIMULATOR\n\n";
/*
while(userInput > 3 || userInput < 1) {
cout << "Enter the attack you want to use!\n";
for(int i=0; i<sizeof(user_inputs)/sizeof(user_inputs[0]); i++) {
cout << i + 1 << ". " << user_inputs[i] << " ";
}
cout << "\n";
cin >> userInput;
};
*/
/*
cout << boss_inputs[boss_index] << " ";
cout << boss_attacks[boss_inputs[boss_index]];
*/
return 0;
}
Any and all help would be appreciated. Thank you!
sizeof(boss_list_holder)/sizeof(boss_list_holder[0])
This is a very old-fashioned way of deducing the length of an array, and indeed it only works for arrays.
It doesn't even work on pointers, which people often have instead of arrays without realising it, so std::size(boss_list_holder) is a better general solution.
In your case, simply boss_list_holder.size() is your best bet.
You should also be using unsigned int (or, ideally, std::size_t) for array or vector indices, and your compiler will warn you about unsafe signed/unsigned comparisons if you stick with int.
(Some people will go further and recommend std::vector<string>::size_type, though personally I think that's overkill.)
Your for loop over the boss_list_holder vector is wrong:
for(int j=0; j<(sizeof(boss_list_holder)/sizeof(boss_list_holder[0])); j++)
This sizeof trick only works for fixed-sized arrays (like your user_input), where the elements are stored directly in the allocated memory of the array itself.
This does not work for dynamic containers like std::vector, which boss_list_holder is an instance of. The elements of a std::vector are not stored in the allocated memory of the std::vector object itself (ie, sizeof(boss_list_holder) is not equal to sizeof(string) * NumberOfStrings). They are stored in an array that is allocated elsewhere in memory, and the std::vector object simply holds a pointer to that array (amonst other things).
You need to use the vector's size() method instead, that will return the number of elements that have been put into the std::vector's inner array:
for(size_t j = 0; j < boss_list_holder.size(); ++j)

How to get a vector of strings from input?

I'm trying to get a vector of string from input to create a graph , but i don't know why in middle my code it crashes. please help me fix it. I use Visual Studio.
#include <iostream>
#include <vector>
#include <iterator>
void main(void)
{
{
using namespace std;
int8_t n{ 0 };
cout << "enter the size of graph : ";
cin >> n;
vector<string> graph(n);
string connectionsWith;
vector<string>::iterator i;
string::iterator e;
int p{ 0 };
for (i = graph.begin(); i != graph.end(); ++i)
{
cout << '\n' << "enter the vertices that are connected to " << p << " : ";
cin >> connectionsWith;
graph.push_back(connectionsWith);
p++;
}
p = 0;
for (i = graph.begin(); i != graph.end(); ++i)
{
cout << p << " is connected to " << *i;
p++;
}
}
}
In your constructor of graph, you allocate n string. Inside the loop you add on top of the constructed n strings, n more strings via push back. That potentially invalidates your iterators, as ChrisMM said, also not the most efficient way to implement such a scenario.
So as a solution, instead of
vector<string> graph(n);
do
vector<string> graph;
graph.reserve(n);
and iterate over the index, e.g. from 0 to n, and push back.
Especially in the first loop you are not dereferencing the iterator at all, which suggests that using index based loop would show your intent better.
life_steal pointed out the problem. I would like to add few information and other way to solve the problem.
int8_t n{ 0 };
vector<string> graph(n); // Here initialization happening with ASCII. If input is "1" then it would be "49". Consider changing int8_t to int if you unaware of that.
graph.push_back(connectionsWith); //Instead of this line Use: *i = connectionsWith; it directly assign the value.

C++ reversing a string

I'm trying to reverse a string in my C++ code line below revStr.at(j) = str.at(size);
But it doesn't change any of the elements in revStr.
Is there another way to do it without using any libraries.
#include <iostream>
#include<sstream>
#include <iterator>
using namespace std;
int main() {
ostringstream d;
long long c = 123456789;
d << c;
//cout << c << endl;
string str = d.str();
//cout << str.at(0) << endl;
int size = str.size() - 1;
//cout << size << endl;
ostringstream e;
e << str;
string revStr = e.str();
for (int i = size; size==0; size--) {
//cout << str.at(size);
int j = 0;
revStr.at(j) = str.at(size);
j++;
} // End For
cout << "Original String is :" << str << endl;
cout << "Reversed String is :" << revStr << endl;
}
Use std::reverse:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string test{"Hello"};
std::cout << "Original string: " << test << std::endl;
std::reverse(test.begin(), test.end());
std::cout << "Reversed string: " << test << std::endl;
return 0;
}
Output:
Original string: Hello
Reversed string: olleH
If you just want to reverse a string, you should use std::reverse, as described by Tyler Lewis. It is the best option.
If you want to learn C++, then writing your own version is good practice.
The line
for (int i = size; size==0; size--)
means “Create a new int called i and set it to size initially. Then, while size is zero, do the following and then decrement size”.
There are three problems with this:
Size is not zero unless you entered a one-character string
Since you never use i, there’s no point in declaring it
Inside the loop you use j which is set to zero each time.
You can fix the first by changing the middle part of the for loop to size >= 0 (but be careful—if you later change it so that size is an unsigned type, because it doesn’t make sense for it to be negative, that code won’t work; it’s generally better to increment going up instead). You can fix the second by using i everywhere in the loop statement, and not changing size. You can fix the third by using i in the loop body, and not declaring a new variable inside the loop.
I noticed you used std::string so I used std function swap and string. Depending on if you consider this as a 'library'. There are several definitions of 'reverse'. You could reverse the word order in a string, or a pure char to char reversal like I wrote. Reversal could also mean changing character case, etc... but this is simply swap first and last. Then swap the 2nd and 2nd to last, then swap the 3rd and 3rd to last, etc...
So some points from your code. You only need to loop half the string length. The swap is from the ith and the ith to last. So the last is numCharacters - 1, thus the ith to last would be Last - i or numCharacters - 1 - i. I believe this is what you intended by using a farLeft(i) and a farRight(j) index.
#include <iostream>
void reverseStringInPlace(std::string &stringToReverse)
{
int numCharacters = stringToReverse.length();
for (int i=0; i<numCharacters/2; i++)
{ std::swap(stringToReverse[i], stringToReverse[numCharacters-i-1]); }
}
int main()
{
std::string stringToReverse = "reversing a string";
std::cout << stringToReverse << std::endl;
reverseStringInPlace(stringToReverse);
std::cout << stringToReverse << std::endl;
return 0;
}
Output:
reversing a string
gnirts a gnisrever
Changes made to the piece of code in question, it works.
for (unsigned int i = size; size >= 0; size--) {
revStr[j] = str[size];
j++;
}

Compile error declaring my char vector array

I've done some self learning in the past with c++ online but gave up, till I bought a textbook on it and giving it another go. In my past research, I never read anything on vector arrays (or maybe I did and don't remember, who knows).
Anyway it says like regular arrays, vector arrays can be created for any data type and I'm trying to get a char vector array going and I'm running into some compile errors take a look.
I want an array of 26 that houses all the letters in the alphabet capitalized. So 65 to 91 I think. If there is and easier way to initialize the array with the letter I'm interested in learning that way.
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <char> vchChar(26, 65);
for (int iii = 0; iii < vchChar.size(); iii++ )
{
for (int jjj = 65; jjj < 91; jjj++)
{
vchChar(iii) = jjj;
cout << "vchChar(" << iii+1 << ") is:\t" << vchChar(iii) << endl;
}
//cout << "vchChar(" << iii+1 << ") is:\t" << vchChar(iii) << endl;
}
return 0;
}
Originally I had square brackets instead of the parenthesise, fixed that and had hoped it would work but that got a whole set of new problems when I tried changing them in the cout statements. When I had them in square brackets it printed out in the terminal fine no compile errors. So now I have the cout statements like
cout << "vchChar(" << iii+1 << ") is:\t" << vchChar[iii] << endl;
I thought vector arrays where incremented by one from the element before it. But all I got when I printed vchChar into the terminal where all 'A's. So I tried playing around with another for loop to assign them by one from the element before it. I got some different outputs then I'd desired, and cant find the right algorithm to do it.
I'll keep at it, but an answer on this post is just as good for me. I have little idea what I'm doing so post everything you've got, but keep in mind that I probably wont have any idea what you're talking about :S. I've probably left something out because I've change the code a bit when troubleshooting, so if there are any question ask, and thank for your time.
I do not see any sense in your code. If I have understood correctly what you need is the following
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
std::vector<char> vchChar(26);
std::iota( vchChar.begin(), vchChar.end() , 'A' );
for ( char c : vchChar ) std::cout << c << ' ';
std::cout << std::endl;
return 0;
}
Or you can write it even the following way
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
std::vector<char> vchChar( 'Z' - 'A' + 1 );
std::iota( vchChar.begin(), vchChar.end() , 'A' );
for ( char c : vchChar ) std::cout << c << ' ';
std::cout << std::endl;
return 0;
}
If your compiler does not support standard algorithm std::iota and the range-based for statement then you can write
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
std::vector<char> vchChar( 'Z' - 'A' + 1 );
for ( char c = 'A'; c <= 'Z'; ++c ) vchChar[c - 'A'] = c;
for ( std::vector<char>::size_type i = 0; i < vchChar.size(); ++i )
{
std::cout << "vchChar[" << i << "] is:\t" << vchChar[i] << std::endl;
}
return 0;
}
Take into account that it is a bad idea to use magic numbers as 65 or 91. For example if the program will run in an IBM mainframe then the result will be unexpected because there is another coding system, that is EBCDIC instead of ASCII.
As for statement
vchChar(iii) = jjj;
then it is invalid. Expression vchChar(iii) means a call of the operator function with one argument that is not defined in class std::vector.
You have at least the following issues in your code:
Setting a vector element is not done by myVector(index), but myVector[index], so basically the operator[]. In this special case, however, you can just push them in a row to the back.
You are trying to print one element of the array with vchChar(iii), but you should use the .at(index) method.
It is not crucial, but in this special case, you could use the iterator pattern to go through the vector rather than dealing with the indexing. Even if you do not do that, it is needless to use iii and jjj for variable names instead of the regular i and j.
I would prefer to use size_t or the vector<char>::size_type for the loop counters as you compare one of them against the vector size.
You are setting the elements more than once because you have a nested loop.
You are needlessly constructing the vector differently than the default.
You are using hard coded integers rather than actual characters.
Therefore, your correct code would look like this:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector <char> vchChar;
for (char c = 'A'; c <= 'Z'; ++c)
vchChar.push_back(c);
for (vector<char>::size_t i = 0; i < vchChar.size(); ++i)
cout << "vchChar(" << i+1 << ") is:\t" << vchChar.at(i) << endl;
return 0;
}
Disclaimer: this is just compilation and runtime fix. I have not dealt with use case and design issues. There are better solutions as I partially mentioned them, but I decided to make your code with the least impact.
You do not need nested loops.
{
vector<char> vchChar;
for (char letter = 'A'; letter <= 'Z'; ++letter) {
vchChar.push_back(letter);
}
for (int i = 0; i < (int) vchChar.size(); ++i) {
cout << vchChar[i];
}
}

Method problems: Deleting duplicate chars in an array

So I am trying to delete duplicate chars in a partially filled array. The array is populated from a file located on my PC. My array population method is working fine; however, my duplicate deleting method is not. Here is my method:
void deleteRepeated(char array[], int* numberUsed)
{
for (int x = 0; x < *numberUsed ; x++)
{
cout << "Positions used: " << *numberUsed << endl;
for (int y = x+1; y < *numberUsed; y++ )
{
cout << "Positions used: " << *numberUsed << endl;
if (array[y] == array[x])
{
cout << "Positions used: " << *numberUsed << endl;
for (int z = y; z < *numberUsed; z++)
array[z] = array[z+1];
y--;
*numberUsed--;
cout << "Positions used: " << *numberUsed << endl;
}
}
}
}
I am passing the entire array, and the number of indices used in that array. The array length is 10, and my tests, I am using 6 out of those 10 with the chars: {'g', 'g', 'n', 'o', 'r', 'e'}. What am I doing wrong?
NOTE: "cout << "Positions used: " << *numberUsed << endl" is being used to check if the method is correctly deleting or not. In the most inner loop where index is z, is where the method starts to go bonkers.
Any help would be much appreciated.
(I wrote the first part of this answer before I read your comment about STL not being allowed, but I'll leave it anyways because I think it's rather neat code.)
You could use the functionality that the C++ standard library makes available to you. Use std::string instead of char arrays (that's nearly always a good idea), then you can do the following (note: C++11 only because of unordered_set and std::begin):
#include <string>
#include <unordered_set>
#include <iostream>
#include <iterator>
std::string uniquechars(const std::string& s) {
std::unordered_set<char> uniquechars(std::begin(s), std::end(s));
std::string newstring(std::begin(uniquechars), std::end(uniquechars));
return newstring;
}
int main() {
std::string teststr("thisisanexamplesentence");
std::cout << "The unique characters of " << teststr << " are " << uniquechars(teststr) << std::endl;
}
Note that it doesn't keep the original order of the characters though, so if that's needed this does not work.
If you have to work without the standard library, you have to dig a bit deeper. #TimChild above already made a good start diagnosing what's wrong with your program, but there are more efficient solutions, for example keeping some kind of record of which characters you have already seen. As you're working with chars, I would consider a bit-field that can hold markers (extra overhead of 256/8=32 bytes) or if that's not too much, just a plain array of bools (extra overhead 256 bytes). As the latter is easier to implement and the code is more legible:
void deleteRepeated(char array[], int *numused) {
bool seenthischar[256] = {false};
char *readpointer = &array[0];
char *writepointer = &array[0];
int length = *numused;
for ( ;readpointer <= &array[0] + length; readpointer++) {
if (seenthischar[((unsigned char) *readpointer)]) {
*numused--;
} else {
seenthischar[((unsigned char) *readpointer)] = true;
*writepointer = *readpointer;
writepointer++;
}
}
}
This only has one loop, so it only has to go through the array once, i.e. its time complexity is linear in the length of the input array.
Every time you find a dup you reduce the number chars used
*numberUsed--;
but remember this controlling the first loop index
for (int x = 0; x < *numberUsed ; x++)
so try this
int count =*numberUsed;
for (int x = 0; x < count ; x++)
this way you visit all the original chars in the array.