Iterate through List using for loop in C++ - c++11

H,
I have a list Booklist and now I want to iterate it for each element and I am doing like this but its not correct so can anybody suggest how to do ?
Book::Notes(string booklist)
{
for(int i = 0; i < booklist.Size(); i++)
{
string B1= booklist[i];
}

for (auto &B : booklist)
{
// Do things with B
std::cout << B << std::endl;
}

Related

basic k way merge: terminate called after throwing an instance of 'std::logic_error'

I am trying to implement basic merge k sorted array algorithm but with strings.
I am getting the following error.
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
The code works fine if the names vector has just 2 sub vectors, but when I add another list in the names vector I am getting the above error.
Whats wrong with the code?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> merge_2_names(vector<string> a, vector<string> b){
int i = 0 ; int j = 0;
vector<string> res;
while(i < a.size() && j < b.size()){
if (a[i].compare(b[j]) < 0){
res.push_back(a[i]);
i++;
}else{
res.push_back(b[j]);
j++;
}
}
while (i < a.size()){
res.push_back(a[i]);
i++;
}
while(j < a.size()){
res.push_back(b[j]);
j++;
}
return res;
}
vector<string> merge_k_names(vector<vector<string>> names){
vector<string> result;
cout << names.size() << "\n";
for (string s: names[0]){
result.push_back(s);
}
for(int i=1;i<names.size();i++)
{
result=merge_2_names(result,names[i]);
}
return result;
}
int main() {
vector<vector<string>> names {{"adam" , "raja" , "zync"},
{"edam" , "some" , "zian"},
{"mike" , "jimm" , "pame"}};
cout << names.size() << "\n";
vector<string> res = merge_k_names(names);
for (string s: res){
cout << s << " ";
}
return 0;
}

Why is the merge printing in reverse order

So, I was learning the Merge Sort Algorithm. and I was amazed to see that the merging is printed in reverse order. You can see I am printing the merge vector v at each step but I don't understand why is it in reverse order. The final answer if perfectly fine.
void merge(vector<int> &left, vector<int> &right, vector<int> &v) {
cout << "merged vector is : \n";
for (auto x : v)
cout << x << " ";
cout << endl;
int l = left.size();
int r = right.size();
int i = 0, j = 0, k = 0;
while (i < l && j < r) {
if (left[i] <= right[j]) {
v[k] = left[i];
i++;
} else {
v[k] = right[j];
j++;
}
k++;
}
while (i < l) {
v[k++] = left[i++];
}
while (j < r) {
v[k++] = right[j++];
}
return;
}
You print the destination vector v at the beginning of each step. The contents and order of the destination vector depends on how you use implement the merge sort algorithm, namely how you split the original vector, how you invoke the merge function and what is the original contents of the source vector. If you want to track the behavior of the merge sort algorithm, you should print the vector after the merge operation.
Note also that:
the index variables i, j, k, l and r should have type size_t.
the return; statement at the end of the function is useless.

Printing the contents of a 2d Vector results without indexing each token as one

I'm attempting to print out the contents of a 2d vector in the same fashion in which they are initialized.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<vector<int > > frontier = {{-1,0}, {1,0}, {0,-1}, {0,1}};
for (int i = 0; i < frontier.size(); i++) {
for (int j = 0; j < frontier[i].size(); j++) {
std::cout << frontier[i][j] << ", ";
}
}
cout << "End of frontier. " << endl;
/* This below is an implementation that I found online but found
no
* way to be able to implement the column reference.
*/
for (int i = 0; i < frontier.size(); ++i) {
for (int j = 0; j < col; ++j) {
cout << frontier[i + j * col] << ' ';
}
cout << endl;
}
}
This is to determine the contents of a 2d vector. So far, this code can print out every index separated by a comma. I, on the other hand, need to write code that will signify where a new vector begins.
output:
-1, 0, 1, 0, 0, -1, 0, 1,
expected output:
{{-1,0}, {1,0}, {0,-1}, {0,1}}
Here's how I might do it:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::vector<int>> frontier = { {-1,0}, {1,0}, {0,-1}, {0,1} };
std::string outerPrefix = "";
std::cout << "{";
for(const auto& outer : frontier)
{
std::cout << outerPrefix << "{";
std::string innerPrefix = "";
for(auto inner : outer)
{
std::cout << innerPrefix << inner;
innerPrefix = ",";
}
std::cout << "}";
outerPrefix = ", ";
}
std::cout << "}";
}
Output: {{-1,0}, {1,0}, {0,-1}, {0,1}}
In the first example I used a range-based for loop. If you're familiar with the concept of foreach in many languages it's basically the same thing. If you don't need an actual index variable it is safer because you don't have to worry about being off by one and indexing outside the container. It also works the same way on containers like map or set where you would need to use iterators rather than an index.
If you were to do the same thing with nested index loops like you had in your original it might look something like this:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::vector<int>> frontier = { {-1,0}, {1,0}, {0,-1}, {0,1} };
std::cout << "{";
for(size_t outer = 0; outer < frontier.size(); ++outer)
{
if (outer != 0)
{
std::cout << ", ";
}
std::cout << "{";
for(size_t inner = 0; inner < frontier[outer].size(); ++inner)
{
if (inner != 0)
{
std::cout << ",";
}
std::cout << inner;
}
std::cout << "}";
}
std::cout << "}";
}

Vector of random integer sets

I'm trying to write a little program to create a vector of random integer sets, but the problem is once the first set is created the program keeps storing the same set of numbers in subsequent iterations. Any help to explain or correct this problem would be very much appreciated. Thanks!
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<set>
#include<vector>
using namespace std;
typedef set<int> Set_I;
typedef set<int>::iterator It;
typedef vector<set<int> > vec_Set;
int random();
void print_set(Set_I s);
void print_vec(vec_Set v);
int main()
{
srand(time(0));
Set_I s;
vec_Set v;
v.resize(4);
for(int i=0; i<4;i++)
{
//cout << s.size() << " " <<endl;
while(s.size()<6)
{
s.insert(random());
}
v[i] = s;
s.empty();
}
//print_set(s);
print_vec(v);
cout << endl << s.size() <<endl << v.size();
system("PAUSE");
}
int random()
{
int r = 1 + rand()%49;
return r;
}
void print_set(Set_I s)
{
for(It it=s.begin(); it!=s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void print_vec(vec_Set v)
{
for(int i=0;i<v.size();i++)
{
cout << "{ ";
for(It j = v[i].begin() ; j != v[i].end() ;j++)
{
cout << *j << " ";
}
cout <<"}";
cout <<endl;
}
}
s.empty() returns a bool stating that the set is empty or not. It has no effect on the members of the set!!
you have to use s.clear() for emptying (clearing) your set.

How many characters need to be added to a string to make a palindrome

I just faced an interview in TCS , My last question was to write an algorithm to find how many characters need to be added in a string to make it a palindrome. I started out, but wasnt able to complete. what would be a way to find that?
String palindrome = "helllllll";
char [] chars = palindrome.toCharArray();
for (int i = 0; i < chars.length; i++) {
int j = 0;
for (; j < chars.length - i; j++) {
if (chars[i+j] != chars [chars.length - 1-j])
break;
}
if (j == chars.length - i) {
System.out.println (i);
break;
}
}
As what Niklas said:
Find the leftmost character in the right half of the string that is a potential "mirror point" of a palindrome. It induces the solution. Also consider even-length palindromes
So as an example code that explains your question, this performs a palindrome test and then print it out in reverse without characters like '!', ', or '?'
And i have marked out the process that answers your question with a caption:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//Variables and arrays
int const index = 30;
char Phrase[index];
char Reverse[index];
char* Palindrome = Reverse;
int i, j;
cout << "Please enter a sentence to be tested as a palindrome: ";
cin.getline(Phrase, 30);
int length = strlen(Phrase);
bool test = true;
for(i = 0, j = length-1; i < j; i++, j--) //Loops from zero to half of the string
{
if(test) // if it is a palindrome so far
{
while(!isalpha(Phrase[i]) && i < j) { i++; }
while(!isalpha(Phrase[j]) && i < j) { j--; }
if(Phrase[i] != Phrase[j]) //Check if the characters match
{
test = false;
}
}
else
{
break;
}
}
if(test)
{
cout << endl << "Phrase/Word is a Palindrome." << endl << endl;
for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
{
*Palindrome = Phrase[j];
}
cout << "The phrase and reverse statement is: " << Reverse << endl << endl;
}
else
{
cout << endl << "Phrase/Word is not a Palindrome." << endl << endl;
}
system("Pause");
return 0;
}

Resources