Why is this code running without vector header file? - c++

I couldn't find out why is the code below working fine on my local system without including the vector header file but not on online judges or online compilers.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
vector<int> v(10);
for(int i = 0; i<10; i++) v[i] = i;
sort(v.begin(),v.end());
for(int i = 0; i<10; i++) cout<<v[i]<<" ";
return 0;
}
I am compiling the code by enabling the warning flags as g++ -Wall -Wextra ./ex.cpp but g++ doesn't give me any warnings at all. Removing the #include<algorithm> does give me the error I wanted, identifier "vector" is undefined, but I don't know what's the relationship between them.

Your algorithm header itself includes the vector header (either directly or indirectly). Because of this, the code after the preprocessor looks the same as if you had included the vector header yourself.
You should not rely on this behavior though, as it depends on the standard library implementation you are using and can change at any time.

Related

Error related to defining and initialization part of an array [duplicate]

#include <iostream>
using namespace std;
int main(){
int n=10;
int a[n];
for (int i=0; i<n; i++) {
a[i]=i+1;
cout<<a[i]<<endl;
}
return 0;
}
worked fine in Xcode4 under Mac
as said in books, it should be wrong, why?
so confused~
This a a C99 feature called VLA which some compilers also allow in C++. It's allocation on stack, just as it would be with int a[10].
That is C99 feature that allows VLA (variable length array).
Compile it with g++ -pedantic, I'm sure that wouldn't compile.

Error with Lambda Expression C++ in VS Code

I'm trying to sort an array m of class measure using a lambda expression as follows:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
struct measure{
int day;
int cow;
int change;
};
int main()
{
int N;
cin >> N;
measure m[N];
for (int i = 0; i < N; i++){
measure m_i;
cin >> m_i.day >> m_i.cow >> m_i.change;
m[i] = m_i;
}
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
}
However, an error occurs when trying to build the task in VS Code (using C++17):
error: expected expression
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
^
1 error generated.
Build finished with error(s).
I've tested this code on other compilers with no difficulties. Why is this error happening on VS Code?
Okay, so it does not look like you are compiling with c++17. I can reproduce this error if I roll back the gcc version to 4.x and leave the standard up to the compiler. Here is how it would look on an older compiler. Here is how it would run properly on a newer compiler. Most likely that you are using something like c++98 or c++03.
Note - I have taken the liberty of modifying your code to make it more C++. Also, please stop with using namespace std.
A simple fix is to just make sure that you are using the right version of the compiler basis the features you need. Lambdas in C++ were introduced in C++11, so clearly you are using a compiler that is at a much lower version.
VS Code does not have a compiler built-in. It uses the system compiler you have installed. You can configure things to use the system compiler and pass the flag --std=c++17 to the compiler command line.
Both gcc and clang++ support this command line flag.

C++ 20 iteratos and g++ standard (std)

Well i just wanted to do a "foreach" like statement in C++, but in reverse order. Reading a little, the doc says that adding this to the "especial" form of the for statment it can be done.
Like this:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vecInt;
for (int i = 0; i < 10; i++)
vecInt.push_back(i);
for (auto const& x : vecInt | std::views::reverse) //Here is the problem with g++ -std=c++2a
{
}
return 0;
}
The thing is that im using ubuntu linux with the latest g++ compiler.
According to what the compiler throws me. I put an "std=c++20", but it then told me: "Did you mean std=c++2a" so, i don't know what the "a" means, but i tought it would be the C++20 standard. And finally discovered that "std::views::reverse" isn't seen by the compiler.
To go to the point: i want to do a reverse "foreach" C++ statement and i can't.
Any help?

unordered_map error in GCC

When was the unordered_map concept built into g++?
Because the following code throws an error.
#include<iostream>
#include<unordered_map>
#include<stdio.h>
using namespace std;
std::unordered_map<std::int,int> mirror;
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
int main(void)
{
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
return 0;
}
I am compiling the code as follows:
g++ -c hashexample.cpp
g++ -o result hashExample.o
./result
The error I got is this:
inavalid types int[char[ for aaray subscript
What is the fix for this?
The problem is your assignment. You cannot assign values to your map in this place. C++ is not a script language.
This program works fine on my machine with gcc4.6:
#include<iostream>
#include<unordered_map>
std::unordered_map<int,int> mirror;
int main() {
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
}
First, as mkaes points out, you cannot put assignments outside functions, so you have to put it in any, for example main.
As for unordered_map, for recent versions of gcc, if you don't want to go into C++11, you can use the TR1 version of unordered_map:
#include <tr1/unordered_map>
and the type std::tr1::unordered_map. You know, C++11 supersedes all this, but you will (at least in GCC) get this working.

C++ code runs with missing header, why?

I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function.
#include<iostream>
using namespace std;
int main()
{
int result;
result = abs(-10);
cout << result << "\n";
return 0;
}
Why does this code still work, even though I forgot the important header (#include<cstdlib>)?
That's because iostream indirectly includes definition for abs(). It is allowed by the Standard, but should not be relied upon, because it's implementation-dependant (i.e. your code may not compile on some other compilers).