Parsing through Vectors - c++11

I am new and learning C++ using the Programming Principles ... book by Bjarne Stroustrup. I am working on one problem and can't figure out how to make my code work. I know the issue is with if (words[i]==bad[0, bad.size() - 1]) in particular bad.size() - 1])
I am trying to out put all words in the words vector except display a bleep instead of any words from the words vector that match any of the words in the bad vector. So I need to know if words[i] matches any of the values in the bad vector.
#include "../std_lib_facilities.h"
using namespace std;
int main()
{
vector<string> words; //declare Vector
vector<string> bad = {"idiot", "stupid"};
//Read words into Vector
for(string temp; cin >> temp;)
words.push_back(temp);
cout << "Number of words currently entered "
<< words.size() << '\n';
//sort the words
sort(words);
//read out words
for(int i = 0; i < words.size(); ++i)
if (i==0 || words[i-1]!= words[i])
if (words[i]==bad[0, bad.size() - 1])
cout << "Bleep!\n";
else
cout << words[i] << '\n';
return 0;
}

You need to go through all of the entries in the bad vector for each entry in the words vector. Something like this:
for(const string& word : words)
{
bool foundBadWord = false;
for(const string& badWord : bad)
{
if(0 == word.compare(badWord))
{
foundBadWord = true;
break;
}
}
if(foundBadWord)
{
cout << "Bleep!\n";
}
else
{
cout << word << "\n";
}
}

Related

Segmentation Fault 11 with Maps and vector c++

I am trying to get a frequency table from a given text.
But the output is showing segmentation fault 11. I don't know why.
I am a newbie. Your help with the code will be very well appreciated.
You can edit my code to make me learn a better method of writing code as well. Thanks a lot.
#include<iostream>
#include<map>
#include<string>
#include<vector>
void make_table(vector<pair<char, int> > &table , string path){
string text = "Hello thusnvkj.ernbuilvgqboipghq3pojavnaj.,fbvlkarebihfg094why091[3tugjvlksbdfv ajklvrpt-30qjhrgiaoehk.BL;H]IH;LGBJSFDNOWI;HBPWRHGB;ORTWIHGOQHRWI0TUGJRLKEWHUGIH49P0-IT302-UR9GM,NXM,BNX,MNMB/E/RGP'KGP34OR[2=O-O-=0-3-1I0-439890375892R0U;L.GNLS.N.SVMS/FS/FKWEP[IF0W))_*(&*(^^&$%#^%$&%*(^*&)(*)_*_(()())))]]'";
map<char, int> m;
for(int i=0; text[i]!='\0'; i++){
m[text[i]]++;
}
map<char,int>::iterator it;
int j=0;
for(it=m.begin();it!=m.end(); it++){
table[j].first=it->first;
table[j].second=it->second;
j++;
//cout << it->first << " " << it->second << endl;
}
return;
}
int main(){
vector<pair<char , int> > table;
string path;
//cin >> path;
path= "Hi";
make_table(table, path);
// make_table function will give us the sorted table(vector of pair) in the decreasing order of frequency of a character.
/*for(int i =0; i<table.size(); i++){
cout << table[i].first << " " << table[i].second << endl;
}
*/
return 0;
}
You get an error because you try to access elements in an empty vector
table[j].first=it->first;
table[j].second=it->second;
Here you can add one line outside the for loop
table.resize(text.size());

Why is problemsPathCoordinate vector of a vector of a pair empty?

I have tried to fill a smaller vector of a vector of pairs with some contents from a bigger vector of a vector of pairs without success. Below is the relevant code with couts and their output. Hopefully this is detailed enough.
/*******************Problems Occur*****************/
int iFirst=problemsStartAt;//first index to copy
int iLast=problemsEndAt-1;//last index -1, 11th stays
int iLen=iLast-iFirst;//10-8=2
//if(problemsStartAt!=0)//I.a
if(problemsStartAt!=0&&problemsEndAt!=0)//I.b
{
v_problem_temp=allPathCoordinates[problemsStartAt];
cout<<"266:"<<v_problem_temp.size()<<endl;
cout<<"267:"<<allPathCoordinates.at(1).size()<<endl;
for(vector<pair<int,int>>::iterator it2=v_problem_temp.begin();
it2!=v_problem_temp.end();
++it2)
{
apair=*it2;
point[apair.first][apair.second]=Yellow;
cout<<apair.first<<","<<apair.second<<endl;
}
problemsPathCoordinate.resize(iLen);
cout<<"iLen*sizeof(problemsPathCoordinate):" <<iLen*sizeof(problemsPathCoordinate)<<endl;
memcpy(&problemsPathCoordinate[0],&allPathCoordinates[iFirst],iLen*sizeof(problemsPathCoordinate));
cout<<"279:problemsPathCoordinate.size():"<<problemsPathCoordinate.size()<<endl;
problemsPathCoordinate.resize(iLen);
memcpy(&problemsPathCoordinate[0],&allPathCoordinates[iFirst],iLen*sizeof(problemsPathCoordinate));
cout<<"283:problemsPathCoordinate.size():"<<problemsPathCoordinate[0].size()<<endl;
cout<<"284:problemsPathCoordinate.size():"<<problemsPathCoordinate[1].size()<<endl;
cout<<"286:allPathCoordinates.size():"<<allPathCoordinates.size()<<endl;
cout<<"287:allPathCoordinates.size():"<<allPathCoordinates.size()<<endl;
//from http://stackoverflow.com/questions/35265577/c-reverse-a-smaller-range-in-a-vector
}
Output:
759: path NOT full-filled, number: 8
755: Problems START here at:8
759: path NOT full-filled, number: 9
700: Problems END here at: 11
266:0
267:0
iLen*sizeof(problemsPathCoordinate):72
279:problemsPathCoordinate.size():3
283:problemsPathCoordinate.size():0
284:problemsPathCoordinate.size():0
286:allPathCoordinates.size():79512
287:allPathCoordinates.size():79512
time:39 seconds
Why are the three problemsPathCoordinate elements empty. How to fix it?
Bo
for (vector< vector > >::iterator it = allPathCoordinates.begin(); it != allPathCoordinates.end(); ++it)
{
allPathCoordinates.erase(allPathCoordinates.begin()+5,allPathCoordinates.end()-2);
v_temp = *it;
//cout<<"v_temp.size():"<
for (vector<pair<int,int> >::iterator it2 = v_temp.begin(); it2 != v_temp.end(); ++it2) {
//v_temp.erase(v_temp.begin()+2);
apair = *it2;
//cout << "(" << apair.first << "," << apair.second << ") ; ";
openPoints[apair.first][apair.second]=0;
closedPoints[apair.first][apair.second]=1;
allObstacles[apair.first][apair.second]=Wall;
point[apair.first][apair.second]=Yellow;
}
/

How to iterate a string using while loop in C++?

number = 100010001111111
for (int i=0; number.length(); i++) {
while number[i] == 1 {
k++;
}
}
I would like to implement a while-loop as a replacement for the for-loop as shown above.
How could I convert this to a while-loop?
Here's a solution for the problem you mentioned in your comment (Problem - 96A)
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your players situation" << endl;
std::string str;
cin >> str;
std::string::size_type i = 0;
int NumbersofAppearances = 0;
int ConsectiveNumberSequence = 7; //You can change that to whatever sequence you like
bool IsDangerous=false;
while (i < str.size())
{
if(str[i]=='1' )
{
++NumbersofAppearances;
//We need to check if we reached the consecutive number or not and save it on a different bool variable
if(NumbersofAppearances>=ConsectiveNumberSequence)
IsDangerous=true;
}
else
{
NumbersofAppearances=0;
}
++i;
}
//print out the end result
if (IsDangerous)
cout <<"YES , this is dangerous"<< endl;
else
cout <<"No, this is not dangerous"<< endl;
return 0;
}
And here's a link to Coding ground

Simple coding to Stack

int main()
{
string sentence;
int length;
cout << "Enter the sentence now." << endl;
getline(cin, sentence);
for(int i = 0; i < sentence[i]; i++)
{
if(sentence[i]==';')
cout<<" ";
else if(sentence[i] != ' ')
{
cout << sentence[i];
}
else if(sentence[i] == ' ')
{
cout << endl;
}
}
}
I need help in this code to change into stack coding method. At least you can show me some clue how to change this code into simple stack code.
cin>>a>>b>>c>>d>>e>>f>>g;
myStack.push(g);
myStack.push(f);
myStack.push(e);
myStack.push(d);
myStack.push(c);
myStack.push(b);
myStack.push(a);
while(!myStack.empty()){
cout<<myStack.top()<<endl;
myStack.pop();
}
return 0;
}
This is an example, but its not flexible. User only can enter 7 words or maybe we can do it in array?

find chars/string in string from vector c++

I have a vector of strings, and I want to count all 'Ace' in the vector. Right now I can only find one...
int main()
{
std::vector<string> vec;
vec.push_back("Ace of Spades");
vec.push_back("Ace");
string value = "Ace";
int cnt = 0;
auto iter = find_if(begin(vec), end(vec), [&](const string &str)
{
return str.find(value) != str.npos;
});
if(iter == end(vec))
cout << "no found" << endl;
else
{
cout << *iter << endl;
cnt++;
cout << cnt++ << endl;
}
}
You could use std::count_if:
auto cnt = count_if(begin(vec),
end(vec),
[&](const string& str) {
return str.find(value) != std::string::npos;
});
Note that this only counts the number of strings containing "Ace", not the total number of occurrences of "Ace" in the vector's elements.
If you just want to count the number of matching elements, you could use std::count_if.
If you also need to do something with them, it would probably be best to forget about the standard library algorithms and use a ranged for like so:
int count = 0;
for (const auto& element : vec) {
if (element.find(value) != std::string::npos) {
std::cout << element << std::endl;
++count;
}
}
std::cout << count << std::endl;

Resources