Ddoes any body know how do do this? Thanks if you do. I am new in c++. I don't know what do do. Python is much better in my opinion.
#include <bits/stdc++.h>
using namespace std;
int main(){
int nums[] = {1, 2, 3, 4};
if (nums == [1, 2, 3, 4]){
cout<<1;
}
}
You should use std::array instead of plain arrays to realize this.
#include <iostream>
#include <array>
int main(void) {
std::array<int, 4> nums = {1, 2, 3, 4};
if (nums == std::array<int, 4>{1, 2, 3, 4}){
std::cout<<1;
}
}
Also see:
c++ - Why is "using namespace std;" considered bad practice? - Stack Overflow
c++ - Why should I not #include <bits/stdc++.h>? - Stack Overflow
From C++17, you can use class template argument deduction to simplify MikeCAT's solution like this:
std::array nums = {1, 2, 3, 4};
if (nums == std::array{1, 2, 3, 4}) {
std::cout << 1;
}
Related
I am trying to find unique element from the array these is question
Input : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output : arr[] = {1, 2, 3, 4, 5}
They give me correct output but why they give 0 at the end in output:
these is my output:
{1,2,3,4,5,0}
Code:
#include<iostream>
using namespace std;
int main(){
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0;j<=n;j++){
if(arr[j]!=arr[j+1]){
cout<<arr[j];
}
}
}
Except for std::cout, you code is much more C than ++.
std::unique of the C++ Standard Library does exactly what you want. There is no need to re-implement this.
Next there is the erase-remove idiom to delete the superfluous elements.
For the output, you can use std::for_each() or at least a range-based for loop.
And also, you don't want using namespace std;
A more modern solution looks like this:
#include <iostream>
#include <algorithm>
#include <vector>
int main(){
std::vector<int> arr {1, 2, 2, 3, 4, 4, 4, 5, 5};
auto last = std::unique(arr.begin(), arr.end());
arr.erase(last, arr.end());
std::for_each(arr.begin(), arr.end(), [](int n){std::cout << n << std::endl;} );
}
Try it on Godbolt.
problem is in for loop becouse you use less or equal to n.
you need to use just less then n becouse array starts from zero that means you asking for sixth element that do not have alocalizated in memory
now it should be correct
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0; j<n; j++) {
if (arr[j]!=arr[j+1]) {
cout<<arr[j];
}
}
}
When I try to change the vector I reach same values. Please explain me how to solve this problem?
#include <iostream>
#include <vector>
using namespace std;
int &Give2DVectorRef(int i, int j, vector<vector<int>> &matrix) {
return matrix.at(i).at(j);
}
int main() {
vector<vector<int>> matrix{
{1, 1, 1, 1, 1}, {2, 2, 2, 2, 2}, {3, 3, 3, 3, 3}};
int ref;
ref = Give2DVectorRef(1, 3, matrix);
ref = 55;
cout << matrix.at(1).at(3) << endl; // print 2, but I expect 55
return 0;
}
The short answer is to change from
int ref;
ref = Give2DVectorRef(1, 3, matrix);
to
int &ref = Give2DVectorRef(1, 3, matrix);
(as #user1810087 already commented)
Here are some more comments on your code
References to vector items can be dangerous.
Image
int & ref = Give2DVectorRef(1, 3, matrix);
matrix[1].erase(matrix[1].begin()+2); //deletes the third item
ref = 55; // Reference is no longer meaningful
This situation might also accour, if you insert an item before the location of the item referenced to.
A matrix should not be stored as a vector of vectors. If you are using this construct in heavy numerical calculation you will face severe performance problems. A single vector of length (N*M) will be faster. Even better is the usage of a library such as Eigen.
If you are concerned about performance, the operator .at is slow in comparison to the operator [].
Avoid using using namespace std;
I would like to find the difference between two lists. For example:
// two lists:
A = [ 0, 1, 2, 3, 4, 5, 6 ];
B = [ 1, 4, 5 ];
// difference between the lists:
C = [ 0, 2, 3, 6 ];
I have done this using the STL-library of C++ as follows:
#include <iostream>
#include <vector>
int main()
{
std::vector<size_t> A = {0, 1, 2, 3, 4, 5, 6};
std::vector<size_t> B = {1, 4, 5};
std::vector<size_t> C;
std::set_difference(A.begin(),A.end(), B.begin(),B.end(), std::inserter(C,C.begin()));
return 0;
}
However, because my application uses mostly Eigen, I now would like to do also this using Eigen. I couldn't find what I was looking for in the documentation nor online.
Note that I specifically want to avoid writing my own function.
Here you go:
#include <iostream>
#include <Eigen/Dense>
int main()
{
using namespace Eigen;
VectorXd a(3), b(1);
VectorXd c(a.size());
a << 1,2,3;
b << 1;
auto it = std::set_difference(a.data(), a.data() + a.size(),
b.data(), b.data() + b.size(),
c.data());
c.conservativeResize(std::distance(c.data(), it)); // resize the result
std::cout << c;
}
The key here is to use Eigen::VectorXd::data() member function, which returns a pointer to the underlying storage, which is itself an iterator that can be passed around to C++ standard library functions.
In c++11 you can do this wonderful syntax:
vector<int> numbers = {1, 2, 3};
Is there a way to concatenate a further initializer list onto an existing vector?
numbers.??? ({4, 5, 6});
or
std::??? (numbers, {4, 5, 6});
You can use std::vector::insert for that:
#include <vector>
vector<int> numbers = {1, 2, 3};
numbers.insert( numbers.end(), {4, 5, 6} );
Use std::vector::insert:
numbers.insert(numbers.end(), {4, 5, 6});
You can use std::vector::insert. Link to example code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1,2,3};
a.insert(a.end(), {4,5,6});
for(int &i : a) {
cout << i << " ";
}
cout << endl;
return 0;
}
i want to add an integer in the all indexes of an array .without using loop .in single ststement.
int a[4]={4,4,4,4};
i want to add 1 in all indexes..
so the out put is
cout<
output:_
5
5
5
5
i relly thnks to all..
#include <functional>
#include <algorithm>
int main()
{
int a[] = {1, 2, 3, 4};
std::transform(a, a + sizeof(a) / sizeof(a[0]), a, std::bind1st(std::plus<int>(), 1));
}
Yet another possibility would be to use an std::valarray:
int aa[] = {4, 4, 4, 4};
std::valarray<int> a(aa, 4);
a += 1;
I can't say I'd really recommend this, but it does work.
How about doing it recursively:
template<typename Itr>
void inc(Itr itr, Itr end)
{
if (itr == end) return;
++(*itr);
inc(++itr,end);
}
int main()
{
int a[4]={4,4,4,4};
inc(a,a + 4);
return 0;
}
This is tail-recusive, so a decent compiler should convert this into a loop.
SSE2 solution (using intrinsics):
__m128i v1 = _mm_set_epi32(4, 4, 4, 4);
__m128i v2 = _mm_set_epi32(1, 1, 1, 1);
__m128i v3 = _mm_add_epi32(v1, v2);
#include <iostream>
using namespace std;
int main()
{
int a[4]={4,4,4,4};
a[0]++,a[1]++,a[2]++,a[3]++;
return 0;
}
If your array can only have 4 elements, you might be able to use the PADDD instruction of the SSE2 instruction-set extension. I have never used this, so i cannot provide more details.
This question might give you more hints on how to do it.