How to assign/read a member of an object in a stack?
struct item{
char opra;
int count;
double operand;
};
stack<item> S;
double test = S.top.operand;
it not works, thanks.
Top is a method, so you should call S.top().operand. I got it to compile like that:
#include <iostream>
#include <stack>
struct item{
char opra;
int count;
double operand;
};
int main(){
std::stack<item> S;
double test = S.top().operand;
return 0;
}
You are just making a typo.
top in stack is a function, I let you here a working example:
#include <iostream>
#include <stack>
using namespace std;
struct item{
char opra;
int count;
double operand;
};
int main()
{
item a;
a.opra = 'a';
a.count = 3;
a.operand = 5.0;
stack<item> S;
S.push(a);
// Stack top
cout << S.top().operand;
return 0;
}
Output:
5
Related
I want to point an array in c++ , is it possible ?
My main code :
#include "ArrayPointerClass.h"
#include "stdafx.h"
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
float arr[2];
ArrayPointerClass::pointingArray(&arr);
return 0;
}
ArrayPointerClass.h
#pragma once
static class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr[2]);
};
ArrayPointerClass.cpp
#include "stdafx.h"
#include "ArrayPointerClass.h"
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr[2]){
float newArray[2] = { 2.2f, 2.2f };
*arr = newArray;
}
I've got this error :
Error 3 error C2653: 'ArrayPointerClass' : is not a class or namespace name c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
Error 3 error C3861: 'pointingArray': identifier not found c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
I know in C++ arrays ,arrays without length defined are not allowed . is it the reason ?
Thanks for your support
There is no way to create a static class in c++. static keyword can be applied to objects and functions.
And each array name is a pointer. Therefore subscripts cannot be given in the parameter. It is sufficient to provide the pointer type.
The modified code which works:
#include <iostream>
#include <string>
using namespace std;
class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr);
};
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr){
float newArray[2] = { 2.2f, 2.2f };
arr = newArray;
}
int main()
{
float arr[2];
ArrayPointerClass obj;
obj.pointingArray(arr);
return 0;
}
*arr = newArray; will not work you can't copy a C array like that .
You could have done memcpy() or std::copy() like;
memcpy( newArray, arr, 2);
std::copy( newArray, newArray+2, arr);
Thanks everybody for your answers (even there were partially working)
I found my self a solution
// PointerArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int* test() {
int size_needed = 2;
int* a = new int[size_needed];
a[0] = 0;
a[1] = 0;
return a;
}
int main()
{
//int arr[2] = { 1, 1 };
int* arr = test();
for (int i = 0; i < 2; i++){
cout << arr[i] << std::endl;
}
cin.get();
return 0;
}
Tell me how to create different strings in one pointer string like array.
see following two program. 1st one give an errors. what is wrong here?
Kindly correct it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string *j={"nilesh",
"rohit",
"samir",};
cout<<j<<endl;
}
#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
Write simply
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s[] = { "nilesh", "rohit", "samir", };
for ( const string &t : s ) cout << t << endl;
}
Also instead of the array you could use standard class std::vector<std::string>
For example
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v = { "nilesh", "rohit", "samir", };
for ( const std::string &s : v ) std::cout << s << std::endl;
}
Why not try it in this way ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string j[]={"nilesh",
"rohit",
"samir"};
cout<<j<<endl;
}
Printing j directly wont print all the three names. You need to print j[0], j[1] ...
I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);
I am trying to pass a pointer into my classes function, have it incremented, and have the variable retain it's value using pointers. Heres my code, it doesnt increment.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int y;
};
test::test(int * currentY):
y(*currentY)
{
}
int test::addTo()
{
y++;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
int * number = &pointedAt;
test t(number);
t.addTo();
cout <<*number;
char f;
cin >>f;
}
}
This should do it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int *y;
};
test::test(int *currentY):
y(currentY)
{}
int test::addTo()
{
++*y;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
test t(&pointedAt);
t.addTo();
cout << pointedAt;
}
}
You have to store a pointer to the integer, so it refers to the same address as the original variable.
I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)