Anonymous
is legal to do something like this below? template<T>f(...) { auto var = std::is_same<T, mytype>::type? f_that_return_mytype():f_that_return_another_mytype(); } and if yes, which is the correct way?
Ammar
It represents the question perfectly. He wrote bullshit
Personally, I don't really want to be rude. He may be a beginner that doesn't have experience to ask the question correctly.
Anonymous
What problem are you trying to solve even? @rohanTthakur
Rohan
#include <iostream> using namespace std; class Array { private: int *array; int size; public: void setValue(int size); void printArray(void); int indexOf(int searched); int search(int searched); void bubbleSort(int _order_); void im_bubbleSort(int _order_) const; }; // setValue(int argument) method declare & initialize array. void Array :: setValue(int size) { this->size = size; array = new int[size]; for(int i = 0; i < size; i++) { cout << "Value at index [" << i << "] --> "; cin >> array[i]; } } // printArray() method prints elements of array. void Array :: printArray(void) { cout << endl; cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]" << endl; else cout << array[i] << ", "; } } // indexOf(int argument) method returns index. int Array :: indexOf(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i; return -1; } // end of indexOf() method. // search(int argument) method return index+1. int Array :: search(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i + 1; return -1; } // end of search() method. // default argument '0' for Ascending Order AND argument '1' for Descending Order. void Array :: bubbleSort(int _order_) { if(_order_ != 0) { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } else { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } // end of bubbleSort() method. // Imutable method for sorting in Ascending & Descending Order void im_bubbleSort(int _order_) void Array :: im_bubbleSort(int _order_) const { if(_order_ != 0) for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } else for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]"; else cout << array[i] << ", "; } }
V01D
Oh God. Code wall
Anonymous
#include <iostream> using namespace std; class Array { private: int *array; int size; public: void setValue(int size); void printArray(void); int indexOf(int searched); int search(int searched); void bubbleSort(int _order_); void im_bubbleSort(int _order_) const; }; // setValue(int argument) method declare & initialize array. void Array :: setValue(int size) { this->size = size; array = new int[size]; for(int i = 0; i < size; i++) { cout << "Value at index [" << i << "] --> "; cin >> array[i]; } } // printArray() method prints elements of array. void Array :: printArray(void) { cout << endl; cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]" << endl; else cout << array[i] << ", "; } } // indexOf(int argument) method returns index. int Array :: indexOf(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i; return -1; } // end of indexOf() method. // search(int argument) method return index+1. int Array :: search(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i + 1; return -1; } // end of search() method. // default argument '0' for Ascending Order AND argument '1' for Descending Order. void Array :: bubbleSort(int _order_) { if(_order_ != 0) { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } else { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } // end of bubbleSort() method. // Imutable method for sorting in Ascending & Descending Order void im_bubbleSort(int _order_) void Array :: im_bubbleSort(int _order_) const { if(_order_ != 0) for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } else for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]"; else cout << array[i] << ", "; } }
Why did you invent an array class that doesn't even do anything special. Why are you leaking memory with new
Anonymous
No. He is writing python in C++
King Phyte 🐍
#include <iostream> using namespace std; class Array { private: int *array; int size; public: void setValue(int size); void printArray(void); int indexOf(int searched); int search(int searched); void bubbleSort(int _order_); void im_bubbleSort(int _order_) const; }; // setValue(int argument) method declare & initialize array. void Array :: setValue(int size) { this->size = size; array = new int[size]; for(int i = 0; i < size; i++) { cout << "Value at index [" << i << "] --> "; cin >> array[i]; } } // printArray() method prints elements of array. void Array :: printArray(void) { cout << endl; cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]" << endl; else cout << array[i] << ", "; } } // indexOf(int argument) method returns index. int Array :: indexOf(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i; return -1; } // end of indexOf() method. // search(int argument) method return index+1. int Array :: search(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i + 1; return -1; } // end of search() method. // default argument '0' for Ascending Order AND argument '1' for Descending Order. void Array :: bubbleSort(int _order_) { if(_order_ != 0) { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } else { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } // end of bubbleSort() method. // Imutable method for sorting in Ascending & Descending Order void im_bubbleSort(int _order_) void Array :: im_bubbleSort(int _order_) const { if(_order_ != 0) for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } else for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]"; else cout << array[i] << ", "; } }
Dafaq🤦‍♂🤦‍♂🤦‍♂
Rohan
I am not coding python
Ammar
#include <iostream> using namespace std; class Array { private: int *array; int size; public: void setValue(int size); void printArray(void); int indexOf(int searched); int search(int searched); void bubbleSort(int _order_); void im_bubbleSort(int _order_) const; }; // setValue(int argument) method declare & initialize array. void Array :: setValue(int size) { this->size = size; array = new int[size]; for(int i = 0; i < size; i++) { cout << "Value at index [" << i << "] --> "; cin >> array[i]; } } // printArray() method prints elements of array. void Array :: printArray(void) { cout << endl; cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]" << endl; else cout << array[i] << ", "; } } // indexOf(int argument) method returns index. int Array :: indexOf(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i; return -1; } // end of indexOf() method. // search(int argument) method return index+1. int Array :: search(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i + 1; return -1; } // end of search() method. // default argument '0' for Ascending Order AND argument '1' for Descending Order. void Array :: bubbleSort(int _order_) { if(_order_ != 0) { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } else { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } // end of bubbleSort() method. // Imutable method for sorting in Ascending & Descending Order void im_bubbleSort(int _order_) void Array :: im_bubbleSort(int _order_) const { if(_order_ != 0) for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } else for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]"; else cout << array[i] << ", "; } }
You should use pastebin, gist or other paste services to post a long code like this.
Anonymous
I am not coding python
Use std::vector for a dynamic array. Use the iterators it provides through .begin() and .end()
Anonymous
And write a sorting algorithm that can work on those iterators
Anonymous
Your algorithm shouldn't depend on the whole class
Anonymous
If you want a static array use std::array
Rohan
i want to make my own methods that can directly work on primitive data types
Anonymous
i want to make my own methods that can directly work on primitive data types
This is how the main code should look like when using the standard library (before C++20) int main() { std::vector<int> vect{1, 0, -6, 9, 4, 2, 0}; std::sort(vect.begin(), vect.end()); // print the vector here }
Rohan
This is what I did
Rohan
yes
Anonymous
Not invent a new paradigm of programming that is not used in C++
Anonymous
I am not inventing anything
Look at the ideal C++ code
Anonymous
Can i put your bubble sort function in place of that std::sort line?
Rohan
#include <iostream> using namespace std; class Array { private: int *array; int size; public: void setValue(int size); void printArray(void); int indexOf(int searched); int search(int searched); void bubbleSort(int _order_); void im_bubbleSort(int _order_) const; }; // setValue(int argument) method declare & initialize array. void Array :: setValue(int size) { this->size = size; array = new int[size]; for(int i = 0; i < size; i++) { cout << "Value at index [" << i << "] --> "; cin >> array[i]; } } // printArray() method prints elements of array. void Array :: printArray(void) { cout << endl; cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]" << endl; else cout << array[i] << ", "; } } // indexOf(int argument) method returns index. int Array :: indexOf(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i; return -1; } // end of indexOf() method. // search(int argument) method return index+1. int Array :: search(int searched) { for(int i = 0; i < size; i++) if(array[i] == searched) return i + 1; return -1; } // end of search() method. // default argument '0' for Ascending Order AND argument '1' for Descending Order. void Array :: bubbleSort(int _order_) { if(_order_ != 0) { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } else { for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } } // end of bubbleSort() method. // Imutable method for sorting in Ascending & Descending Order void im_bubbleSort(int _order_) void Array :: im_bubbleSort(int _order_) const { if(_order_ != 0) for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } else for(int i = 0; i < size; i++) { int temp; temp = 0; for(int j = 0; j < size; j++) { if(array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } cout << "["; for(int i = 0; i < size; i++) { if(i == size - 1) cout << array[i] << "]"; else cout << array[i] << ", "; } }
so this is wrong
Anonymous
so this is wrong
Not exactly wrong. But overcomplicated and not C++ like at all.
Anonymous
As we said here, doing python in C++
Asdew
Hahah, Rose reported you.
V01D
Hahah, Rose reported you.
The message was forwarded and rose reported that message so technicallllyyy...
Anonymous
sorry, I wrote the wrong code
Anonymous
yes using the value
V01D
This is what I did
Windows 7 is a liability. It has reached EOL and you may be extremely vulnerable
Anonymous
template<T>f(...) { auto var = std::is_same<T, mytype>::value? f_that_return_mytype():f_that_return_another_mytype(); }
Anonymous
I have an error
nemesic
help me
Anonymous
👍, yes true is this
nemesic
nemesic
#include <iostream> #include "windows.h" using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int a, x, b, e; cout << "Enter the number:" << endl; cin >> x >> a >> b >> e; if (x < 0) cout << sin(x) << endl; else if (x = 0) cout << (pow((2 * e), a * x)) << endl; else if (x = x > 0, x < 8) cout << (pow((b * x + a), -1)) << endl; system("pause"); return 0; }
nemesic
this true?
nemesic
I just almost do not understand, I am a beginner (
nemesic
i am 1 course first time c ++
Anonymous
Hahah, Rose reported you.
Yeye that guy couldn't join. So i forwarded the message and tagged admemes
V01D
what you mean by that???
Windows 7 is not a safe OS. Update to windows 10 if you can
Anonymous
how do i specify T to be both an std::output_iterator and an std::forward_iterator?
Rohan
May I DM you???
Rohan
Oh'k dhanyewaad
V01D
Uhh. Google memory segment layout bootsector
V01D
I kinda wanna know too
V01D
I think an OS can choose what memory to segment. Like with paging or something
Anonymous
no i want T to satisfy both output iterator and forward iterator requirements
Talula
Uhh. Google memory segment layout bootsector
Depends on the processor type 16 bit had 65536... but now I think it's all direct access... there are no segments. Segments were originally created for 8 bit addressing system.
Anonymous
no i want T to satisfy both output iterator and forward iterator requirements
The type std::iterator_traits<It>::reference must be exactly T& if It satisfies OutputIterator (It is mutable) const T& otherwise (It is constant), there is this named requirements test. but i wanted to merge the two concepts directly
Anonymous
no. a forwarditerator can be dereferenced to an lvalue only if the iterator also satisfies outputiterator
Anonymous
oh sorry. i forgot that const T& is also an lvalue
Anonymous
i meant assignable
Anonymous
if x is a forward iterator, then *x is assignable to only if x also satisfies output iterator
Anonymous
no. that it shouldn't be read-only. i want to guarantee that i can write to the dereferenced object.
Anonymous
*x = value; should work for some forward iterator x
Anonymous
what else does being an output iterator guarantee
Anonymous
no ;_;
olli
what else does being an output iterator guarantee
by implementing these functions your iterator should satisfy it? bool operator==(const Iterator& rhs) const; bool operator!=(const Iterator& rhs) const; // forward iterator Iterator operator++(int); Iterator& operator++(); const_reference operator* () const; const_pointer operator->() const; Iterator operator+ (difference_type v) const; // output iterator reference operator* (); pointer operator->();
Anonymous
no they are not required to do so. https://en.cppreference.com/w/cpp/named_req/OutputIterator output iterators are about writing to the pointed to object
Anonymous
why not just use these functions then? If T does not satisfy the requirements the compilation will fail?
yeye but concepts are supposed to get rid of 20,000 page template errors, that's why i wanted to use them
Anonymous
just somehow merge the two concepts into one and use it
Anonymous
yes. if T is not an output iterator (for example cbegin(), cend() combo), throw a template error using concepts
Anonymous
oh i just googled "how to write concepts C++" and found the simplest answer - template<typename T> concept output_forward_iterator = std::output_iterator<T> && std::forward_iterator<T>; Edit for future reference - this is wrong as std::output_iterator needs two arguments. It is trivial to change this. However, the standard already provides iterator concepts related to permutation and sortability
Anonymous
https://godbolt.org/z/938Me7
Anonymous
By object i assumed the dereferenced object
Anonymous
Why would i even talk about assigning to the iterator itself. It is trivial.
Anonymous
Do we need delete after new now for regular user-defined data structures, e.g., TreeNode? If we just quit the program without delete, will there be memory leak?
Anonymous
Thanks Sir. I saw the Cpp answers on leetcode have new but not delete. I thought now delete is optional now.
Anonymous
E can be written into ≈ the operator=() member function exists You were talking about streams and operator<< and forward iterators guaranteeing output iterator requirements/forward iterators being stronger than output iterators