Segmentation Fault 11 with Maps and vector c++ - c++11

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());

Related

How to use OpenMP to deal with two for loops with

I am new to OpenMP... Please help me with this dumb question. Thank you :)
Basically, I want to use OpenMP to speed up two for loops. But I do not know why it keeps saying: invalid controlling predicate for the for loop.
By the way, my GCC version is gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and OS I am using is Ubuntu 16.10.
Basically, I generate a toy data that has a typical Key-Value style, like this:
Data = {
"0": ["100","99","98","97",..."1"];
"1": ["100","99","98","97",..."1"];
...
"999":["100","99","98","97",..."1"];
}
Then, for each key, I want to compare its value with the rest of the keys. Here, I sum them up through "user1_list.size()+user2_list.size();". As for each key, the sum-up process is totally independent of other keys, which means this works for parallelism.
Here is my toy example code.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include "omp.h"
using namespace std;
int main(){
// Create Data
map<string, vector<string>> data;
for(int i=0; i != 1000; i++){
vector<string> list;
for (int j=100; j!=0; j--){
list.push_back(to_string(j));
}
data[to_string(i)]=list;
}
cout << "Data Total size: " << data.size() << endl;
int count = 1;
#pragma omp parallel for private(count)
for (auto it=data.begin(); it!=data.end(); it++){
//cout << "Evoke Thread: " << omp_get_thread_num();
cout << " count: " << count << " / " << data.size() << endl;
count ++;
string user1 = it->first;
vector<string> user1_list = it->second;
for (auto it2=data.begin(); it2!=data.end(); it2++){
string user2 = it2->first;
vector<string> user2_list = it2->second;
cout << "u1:" << user1 << " u2:" << user2;
int total_size = user1_list.size()+user2_list.size();
cout << " total size: " << total_size << endl;
}
}
return 0;
}

C++ MurmurHash3 returning same value for different key

I am confused with how should i call MurmurHash3_x86_128() when i have lot of key value. The murmurhash3 code can be found https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp. Method definition is given below.
void MurmurHash3_x86_128 ( const void * key, const int len,
uint32_t seed, void * out )
I am passing different key value using a for loop as shown below but still the hash value return is same. If i am removing for loop and passing individual key value then the value is different. What am i doing wrong ?
int main()
{
uint64_t seed = 100;
vector <string> ex;
ex.push_back("TAA");
ex.push_back("ATT");
for(int i=0; i < ex.size(); i++)
{
uint64_t hash_otpt[2]= {};
cout<< hash_otpt << "\t" << endl;
const char *key = ex[i].c_str();
cout << key << endl;
MurmurHash3_x64_128(key, strlen(key), seed, hash_otpt); // 0xb6d99cf8
cout << hash_otpt << endl;
}
return 0;
The line
cout << hash_otpt << endl;
is emitting the address of hash_otpt, not its contents.
It should be
cout << hash_otpt[0] << hash_otpt[1] << endl;
Basically the 128-bit hash is split and stored in two 64-bit unsigned integers (the MSBs in one and the LSBs in another). On combining them, you get the complete hash.

Parsing through Vectors

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";
}
}

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

searching a string for particular words

i want to create like a very basic AI for a console game. i need a function that will recognize 2 or 3 words within a string created via cin. any help would be greatly appreciated as iv been at this for a while now and cant seem to find a solution.
#include <string>
#include <iostream>
using namespace std;
//-------------------------------------------globals
string sentence;
int main()
{
getline(cin, sentence);
string search;
size_t pos;
search = "hi";
pos = sentence.find(search);
if (pos != string::npos)
cout << "hello sir " << endl;
else
search = "cya";
pos = sentence.find("cya");
if (pos != std::string::npos)
std::cout << "goodbye sir " << endl;
else
search = "goodbye";
pos = sentence.find("goodbye");
if (pos != std::string::npos)
std::cout << "shuting down sir " << endl;
else
return 0;
}
what i would like is to have the initial search look for other words such as hello hey, and so forth, and the similer thing with the others

Resources