Can't assign value to vector in c++ [duplicate] - c++

This question already has an answer here:
Does clang on Mac not support uniform initialization?
(1 answer)
Closed 4 months ago.
I am trying to assign value to a vector but I keep getting different errors. I am using clang++ version 14.0.0 to build the file and I am getting the error using vs code debugger.
Here are the different erros:
When i run this code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v = {0, 1};
return 0;
}
I get the error "expected expression".
When i run this code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v = vector<int>{0, 1};
return 0;
}
I get error "expected '(' for function-style cast or type construction"
Neither initiliazing nor assigning the vector later seems to work.
If my problem is unclear. Please let me know how I can improve :)

clang++ on Mac defaults to using C++98 (-std=c++98) and in C++98 both errors are to be expected.
Just add
-std=c++11
(or later, like -std=c++14, -std=c++17 or -std=c++20) when compiling and both your snippets will compile fine.

Related

Using std::vector does not work

Why the following code doesn't compile,
#include <vector>
using std::vector;
vector<int> v; // Error: too few template arguments, expected 2
but the same code with map (and pair, set, ...) instead of vector works?
#include <map>
using std::map;
map<int, int> m; // OK
And also this code works fine:
#include <vector>
using namespace std;
vector<int> v; // OK
I know that constructor of std::vector has two arguments (type and allocator), but why vector behaviour is so different from other containers?
UPD: I'm sorry, this is my mistake. Actually the code does compile, but CLion marks it as an error. So it is CLion's bug.
It is yet not fixed CLion bug: https://youtrack.jetbrains.com/issue/CPP-5758#u=1454575544687.
As a workaround you can try to use libstdc++ instead of libc++, see https://youtrack.jetbrains.com/issue/CPP-5758#comment=27-2389700.

c++: vector does not name a type [duplicate]

This question already has an answer here:
Does not name a type
(1 answer)
Closed 5 years ago.
very easy but frustrating question from me.
#include <vector>
#include <iostream>
using namespace std;
vector <int> queue;
queue.push_back(2);
int main(){
cout << queue[0] <<endl;
}
compiled with g++ -std=c++11 Cpp_test.cpp, return error
Cpp_test.cpp:51:1: error: ‘queue’ does not name a type
queue.push_back(2);
Can anyone help? Thanks a lot!
queue.push_back(2); should go in main.
To clarify, you cant just place code arbitrarily and have it executed. Declarations are fine outside of main, but that's not a declaration.
Jay is correct. However, since you're using C++11, you can keep your "initialisation" near the declaration by actually making it an initialisation:
vector<int> queue = {2};
(live demo)
By the way, std::vector is a strange choice for a queue.

Error "function count_if() could not be resolved" - C++

I am using Eclipse for C++ testing and I am getting an error related to the count_if() function.
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> v{3,4,5,6,7,8,9,10,100,12,2};
auto isodd=[](int x){return x % 2;};
std::count_if(begin(v),end(v),isodd(3));
return 0;
}
While compiling this program Eclipse gives the following error: "function count_if() could not be resolved".
Thanks for any kind of hint.
I have done very silly mistake.By adding #include < algorithm>
I've solved my problem.

How to use C++11 std::stoi with gcc? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.

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.