Here is the code:
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdio>
int main(int argc, char *argv[]) {
const unsigned int max_chars = 100;
char buffer[max_chars];
std::cin.getline(buffer, max_chars, '\n');
unsigned int count = 0;
for (auto c : buffer) {
if (c == '\0') {
break;
}
count++;
}
std::cout << "Input: ===========" << std::endl;
std::cout << buffer << std::endl;
std::cout << "Number of chars ==" << std::endl;
std::cout << std::dec << count << std::endl;
std::cout << "==================" << std::endl;
}
This is adapted from some example code in a c++ text book deliberately dealing with c-style strings, so bear with me.
So I tried two versions of this, one with for (auto c : buffer) and the other with for (auto &c : buffer). Both seemed to work. The question is, what is the difference then?
When you use a link, you work directly with the elements of the container. Otherwise - with a copy. Try this example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n = 10;
vector<int> a(n);
vector<int> b(n);
for (auto &key : a) key = rand()%10;
for (auto key : b) key = rand()%10;
for (int i = 0; i < n; i++) cout << a[i];
cout << endl;
for (int i = 0; i < n; i++) cout << b[i];
}
The first one (no &) is a value, the second one (with &) is a reference. A reference, as its name implies, "references" a value, similar to the way a pointer "points" to a value.
Try adding c = 'x'; after your if statement and trying both ways to see the difference here.
Related
I want to increase the size of the array of string after declaring it once, how can it be done. I need to increase the size in the following code..
#include<iostream>
using namespace std;
#include<string>
int main()
{
int n;
string A[] =
{ "vaibhav", "vinayak", "alok", "aman" };
int a = sizeof(A) / sizeof(A[0]);
cout << "The size is " << a << endl;
for (int i = 0; i < a; i++)
{
cout << A[i] << endl;
}
cout << "Enter the number of elements you want to add to the string"
<< endl;
cin >> n;
cout << "ok now enter the strings" << endl;
for (int i = a; i < n + a; i++)
{
cin >> A[i];
}
a = a + n;
A.resize(a); // THIS KIND OF THING
for (int i = 0; i < a; i++)
{
cout << A[i] << endl;
}
return 0;
}
Plain and simple: you cannot.
You can get a larger array, copy all your stuff over and use that instead. But why do all that, when there is a perfectly good class already there, doing it all for you: std::vector.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> A = {"vaibhav", "vinayak", "alok", "aman"};
std::cout << "The size is " << A.size() << std::endl;
for(string s : A)
{
std::cout << s << std::endl;
}
// want to enter more?
sd::string more;
std::cin >> more;
A.push_back(more);
std::cout << "The size is " << A.size() << std::endl;
for(string s : A)
{
std::cout << s << std::endl;
}
return 0;
}
Convert your code over to use std::vector and this problem becomes much easier to solve.
#include<iostream>
#include<string>
#include<vector>
int main(){
int n;
std::vector<std::string> A = {"vaibhav", "vinayak", "alok", "aman"};
int a = A.size();
std::cout << "The size is " << a << std::endl;
//Prefer Range-For when just iterating over all elements
for(std::string const& str : A){
std::cout << str << std::endl;
}
std::cout << "Enter the number of elements you want to add to the string" << std::endl;
std::cin >> n;
std::cout << "ok now enter the strings" << std::endl;
for(int i = 0; i < n; i++ ) {
//emplace_back automatically resizes the container when called.
A.emplace_back();
std::cin >> A.back();
//If you're using C++17, you can replace those two lines with just this:
//std::cin >> A.emplace_back();
}
for(std::string const& str : A){
std::cout << str << std::endl;
}
return 0;
}
Also, don't use using namespace std;, since it leads to expensive to fix bugs and makes your code harder to read for other C++ programmers.
I want to increase the size of the array of string after declaring it
once, how can it be done.
It cannot be done. Use std::vector if the element count isn't known at compile time or can change dynamically. It even has a resize member function named exactly like the one in your code.
You cannot increase the size of a Raw Array, you could use an std::vecto<std::string> as this type of array can grow at runtime.
However, you could also create a class that will store an array of string and create your own implementation to resize the raw array. Which would be creating a bigger array and copying all the other values over, then setting the class array to the new array (or just return it)
This program reverses a given string but i can't seem to figure out how to return the entire buffer, all answers welcome.
#include <iostream>
#include <string.h>
using namespace std;
char ReverseString(char *input);
int main()
{
string str;
cout << "Please Enter a String: ";
getline(cin,str);
cout << "The string you entered is: " << str << endl;
str = ReverseString(&str[0]);
cout << str;
}
char ReverseString(char* input)
{
int size = strlen(input);
char *it;
char *revBuffer = new char[size];
it = revBuffer;
for(int i=0; i<size; i++)
{
*it = input[(size-1)-i];
it++;
}
return *revBuffer; //
I can't seem to figure out how to return the entire buffer instead of just one element the pointer is pointing to.
For starters it does not make sense to declare an object of the type std::string and then to try to reverse it as a character array.
And moreover the header that contains declarations of standard C functions is named like <cstring>.
Also you need to include the header that contains the definition of the class std::string.
So the headers should be
#include <iostream>
#include <string>
#include <cstring>
However a question arises: why not to use the same object of the type std::string instead of interpreting it as a character array?
Your approach even if to make it to compile has in any case bugs. Due to this statement
str = ReverseString(&str[0]);
there is a memory leak. And moreover the returned string is not zero-terminated.
Thus the program has undefined behavior.
In fact what you are trying to do is to copy one string in another string in the reversed order.
Using objects of the type std::string the task can be done in one line.
For example
#include <iostream>
#include <string>
int main()
{
std::string s("Hello, World!");
std::string t( s.rbegin(), s.rend() );
std::cout << s << std::endl;
std::cout << t << std::endl;
return 0;
}
The program output is
Hello, World!
!dlroW ,olleH
if you want to reverse the original string then you could write
#include <iostream>
#include <string>
int main()
{
std::string s("Hello, World!");
std::cout << s << std::endl;
s.assign( s.rbegin(), s.rend() );
std::cout << s << std::endl;
return 0;
}
You can write a separate function the following way
#include <iostream>
#include <string>
std::string ReverseCopyString( const std::string &s )
{
return { s.rbegin(), s.rend() };
}
int main()
{
std::string s("Hello, World!");
std::string t = ReverseCopyString( s );
std::cout << s << std::endl;
std::cout << t << std::endl;
return 0;
}
Take into account that there is standard algorithm std::reverse_copy that does the same task. Or if you want to reverse the original string then you can use the algorithm std::reverse.
For example
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main()
{
std::string s("Hello, World!");
std::string t;
t.reserve(s.size());
std::reverse_copy(s.begin(), s.end(), std::back_inserter(t));
std::cout << s << std::endl;
std::cout << t << std::endl;
return 0;
}
If you want to deal with character arrays then the original string should also be stored in a character array.
#include <iostream>
#include <string>
#include <cstring>
char * ReverseCopyString(const char *s)
{
size_t n = std::strlen(s);
char *t = new char[n + 1];
size_t i = 0;
for (; i != n; i++) t[i] = s[n - 1 - i];
t[i] = '\0';
return t;
}
int main()
{
const size_t N = 100;
char s[N];
std::cout << "Please Enter a String: ";
std::cin.getline(s, sizeof( s ) );
std::cout << "The string you entered is: " << s << std::endl;
char *t = ReverseCopyString( s );
std::cout << t << std::endl;
delete []t;
return 0;
}
The program output might look like
Please Enter a String: Hello, World!
The string you entered is: Hello, World!
!dlroW ,olleH
You are returning a char instead of a char*. A char is always just a single character. If you want to get a string back return a string or a char*.
You are already using std::string, so why not use it to reverse the string?
#include <iostream>
#include <string>
std::string ReverseString(std::string const &str)
{
return std::string(str.crbegin(), str.crend());
}
int main()
{
std::string str("1234");
std::cout << ReverseString(str);
return 0;
}
Prints
4321
You can do two things. Either you can choose to return a char*. The code of which is below:
#include <iostream>
#include <string.h>
using namespace std;
char *ReverseString(char *input);
int main()
{
string str;
cout << "Please Enter a String: ";
getline(cin,str);
cout << "The string you entered is: " << str << endl;
str = ReverseString(&str[0]);
cout << str;
}
char *ReverseString(char* input)
{
int size = strlen(input);
char *it;
char *revBuffer = new char[size];
it = revBuffer;
for(int i=0; i<size; i++)
{
*it = input[(size-1)-i];
it++;
}
return revBuffer;
}
Or, you can print the string in your fuction only. The code of which is:
#include <iostream>
#include <string.h>
using namespace std;
void ReverseString(char *input);
int main()
{
string str;
cout << "Please Enter a String: ";
getline(cin,str);
cout << "The string you entered is: " << str << endl;
ReverseString(&str[0]);
}
void ReverseString(char* input)
{
int size = strlen(input);
char *it;
char *revBuffer = new char[size];
it = revBuffer;
for(int i=0; i<size; i++)
{
*it = input[(size-1)-i];
it++;
}
cout << revBuffer;
}
The code is self explanatory.
Your function is set to return a single character char and that's what you've done, you've returned the first char of the reversed string by de-referencing the revBuffer pointer which is a pointer to the first element of the char array.
What you need to do is to set your function to return a pointer char* instead of a char and so you can return revBuffer.
So your code should look like this :
#include <iostream>
#include <string.h>
using namespace std;
//Function return a char pointer instead of a char
char* ReverseString(char *input);
int main()
{
string str;
cout << "Please Enter a String: ";
getline(cin,str);
cout << "The string you entered is: " << str << endl;
str = ReverseString(&str[0]);
cout << str;
}
char* ReverseString(char* input)
{
int size = strlen(input);
char *it;
char *revBuffer = new char[size+1];
it = revBuffer;
for(int i=0; i<size; i++)
{
*it = input[(size-1)-i];
it++;
}
*it = '\0';
//return a pointer to the reversed string
return revBuffer;
}
I have this c++ code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct szemelyadat {
string vnev;
string knev;
int szulev;
int szulhonap;
int szulnap;
};
int vfmeret(const char* fajlnev)
{
string temp;
int merettemp = 0;
ifstream meretBE(fajlnev);
while (!meretBE.eof()) {
meretBE >> temp;
merettemp++;
}
meretBE.close();
return merettemp;
}
void vfbeolv(szemelyadat* vek, int elemszam, const char* fajlnev)
{
int i, j;
string stemp, stempresz;
ifstream adatBE(fajlnev);
for (i = 0; i < elemszam; i++) {
adatBE >> stemp;
istringstream sorfolyam(stemp);
j = 0;
while (getline(sorfolyam, stempresz, ';')) {
if (j == 0)
vek[i].vnev = stempresz;
if (j == 1)
vek[i].knev = stempresz;
if (j == 2)
vek[i].szulev = atoi(stempresz.c_str());
if (j == 3)
vek[i].szulhonap = atoi(stempresz.c_str());
if (j == 4)
vek[i].szulnap = atoi(stempresz.c_str());
j++;
}
}
adatBE.close();
}
void vfkiir(szemelyadat* vek, int elemszam)
{
cout << "Vezeteknev "
<< "Keresztnev "
<< "Szuletesiev "
<< "Szuletesihonap "
<< "Szuletesinap " << endl;
for (int i = 0; i < elemszam; i++)
cout << vek[i].vnev << " " << vek[i].knev << " " << vek[i].szulev << ". " << vek[i].szulhonap << ". " << vek[i].szulnap << endl;
}
int main()
{
int n = vfmeret("szuletesi_nevlista.txt");
szemelyadat* vektor;
vektor = new szemelyadat[n];
vfbeolv(vektor, n, "szuletesi_nevlista.txt");
vfkiir(vektor, n);
delete[] vektor;
cin.get();
cin.get();
return 0;
}
I need to cout the count of the unique elements which is in the "vek[i].szulev"
please help me somebody, how can I do this ?
Here's one simple way to count the unique values in a vector:
using Size = ptrdiff_t;
template< class Item >
auto n_unique_items( vector<Item> const& v )
-> Size
{ return set<Item>{ v.begin(), v.end() }.size(); }
Since you insisted on using "your code", you can still utilize std::set to do the counting.
It all boils down to this:
#include <set>
#include <iostream>
void count_items(szemelyadat* vek, int elemszam)
{
std::set<int> counter;
for (int i = 0; i < elemszam; i++)
counter.insert(vek[i].szulev);
std::cout << "The number of unique items is " << counter.size();
}
If you call the count_items once you fill your "vektor", then this will give you then unique number of szulev in the vektor.
#include <iostream>
#include <vector>
#include <iterator>
#include <iostream>
#include <cmath>
#include <string>
#include <utility>
#include <cstring>
#include <list>
using std::vector;
using std::cout;
using std::list;
using std::endl;
using std::string;
using namespace std;
template <typename T>
void showContents(T& input)
{
typename T::iterator it;
for (it=input.begin(); it != input.end(); it++)
{ cout << *it << " "; }
cout << endl;
}
int main()
{
int B[10] = {0,1,2,3,4,5,6,7,8,9};
cout<< "The first array is: "<< "\n";
int i;
for (i = 0; i<10; i++)
{cout<< B[i]<< " ";}
vector<int> KVec(B,B+10);
cout << "\n \n" << "The first vector is: " << endl;
showContents(KVec);
list<int> BList(B,B+10);
cout << "\n" << "The first list is: " << endl;
showContents(BList);
int BCopy [10];
cout<< "\n" <<"The second array is: "<< endl;
for(int i = 0; i <10; i++)
{
BCopy[i] = B[i];
BCopy[i] += 2;
cout<< BCopy[i]<< " ";
}
vector<int> KVec2(KVec);
cout<< "\n \n" << "The second vector is: "<< endl;
for (int i = 0; i<KVec2.size(); i++){
KVec2[i] += 3;
}
showContents(KVec2);
cout<< "\n" << "The second list is: "<< endl;
std::list<int> BList2 (BList);
for (std::list<int>::iterator b = BList.begin(); b!=BList.end(); ++b)
{
( *b += 5 );
showContents(BList2);
}
This is the code I have. I was able to correctly copy all the arrays, vectors , and lists and increasing the values of those accordingly. The only one I have not been able to increment in the list. My goal is to increment all the elements of the second list by 5. I have been using mulitple references to try and do it but I have tried everything and can not get it to work. Below I have my latest attempt at trying to increment all the values but that doesn't seem to work either so now I need help. That is the only thing left to do in this assignment so any help would be appreciated. Thank you.
Since my comment fixed your problem, I am converting it into an answer.
You copy constructed BList2 using values from BList (I am changing to brace initialization to avoid Most vexing parse). But then, you are iterating over values of BList again. Also, you don't need parentheses around *b += 5. Finally, your showContents function is probably meant to be outside of the loop.
std::list<int> BList2 {BList};
for (std::list<int>::iterator b = BList2.begin(); b != BList2.end(); ++b)
{
*b += 5;
}
showContents(BList2);
boost range transform requires const & for ranges in arguments.
#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>
int main(int argc, char *argv[])
{
using namespace std;
vector<vector<int>> rt0(10,vector<int>(15,2));
vector<vector<int>> irt(10,vector<int>(15,5));
for(auto & i:rt0) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
cout << "\n";
for(auto & i:irt) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
boost::transform(rt0,irt,rt0.begin(),
[] (const vector<int> &t0,const vector<int> &it) {
auto tt = t0;
boost::transform(t0,it,tt.begin(), plus<int>());
return tt;
}
);
cout << "\n";
cout << "\n";
for(auto & i:rt0) {
for(auto& j:i) cout << j << " ";
cout << "\n";
}
return 0;
}
compile and run with
g++ -std=c++11 main.cc; ./a.out
if boost::transform's BinaryOperation took & instead of const & for SinglePassRange1 then i wouldn't have needed to create a copy (auto tt = t0) and just use to instead of tt. is there a way to avoid creating this copying (while still using ranges)?
link to boost range transform: http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/range/reference/algorithms/mutating/transform.html
I need to use for_each either with a tuple or the new boost implementation that takes two arguments.
conceptually transform should not modify. it comes from functional programming where there is no in-place modification.