Lately I have set myself to learn C++, and while working on a bigger project, I have tried to use 'vectors'. But every-time I try passing it a value, it exits with a segmentation fault.
Here is my terminal output:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> test;
cout << "hello world" << endl;
test[0] = 0;
return 0;
}
me#my-MacBook-Pro Desktop % g++ test.cpp -o o && ./o
hello world
zsh: segmentation fault ./o
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> test;
cout << "hello world" << endl;
//test[0] = 0;
return 0;
}
me#my-MacBook-Pro Desktop % g++ test.cpp -o o && ./o
hello world
me#my-MacBook-Pro Desktop %
The segfault is because of out of bound access.
you need to set the size in the ctor
vector<int> test(1);
or push_back:
vector<int> test;
test.push_back(0);
Size it vector<int> test = {0,1,2,3,4}; or vector<int> test(5)
But you might want to use push_back in this situation
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> test;
cout << "hello world" << endl;
test.push_back(0);
cout << test[0];
return 0;
}
Basically adds an item at the end.
Can also use maps with the keys be ints if you want to be able to just [] it or leave in spaces (which from what i saw is what your trying to do)
#include <iostream>
#include <unordered_map>
using namespace std;
int main(){
unordered_map<int, int> test;
cout << "hello world" << endl;
test[0] = 0;
cout << test[0];
return 0;
}
Related
I would like to print the strings at the top of columns with a 1 x 3 array.
I have edited this simple function several times, and this produces the least errors. New to C++, reading Deital Chap 6 Recursive.
What am I missing? I started with half brackes around strings, and brackets seemed to produce less errors.
Here is the code:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main() {
array a[1][3] = ["Car" "Hours" "Charge"]
cout<< a << endl;
}
Terminal produces errors as such:
parking_charges_6_12.cpp: In function ‘int main()’:
parking_charges_6_12.cpp:8:7: error: missing template arguments before ‘a’
8 | array a[1][3] = ["Car" "Hours" "Charge"]
^
This should work:
#include <array>
#include <iostream>
#include <string>
int main(){
std::array<std::string, 3> headlines = {"Car", "Hours", "Charge"};
for( auto const& elem : headlines ){
std::cout << elem << "\t";
}
}
It should be curly braces {} in the initializer, not []. And you need a comma between each element.
On the other hand, in later C++ revisions array can detect the type and number of elements, so you don't have to give that.
#include <iostream>
#include <array>
using namespace std;
int main() {
array a = {"Car", "Hours", "Charge"};
for (auto& item : a)
cout<< item << endl;
}
How about something like this:
#include<iostream>
using namespace std;
int main() {
string data[3] = {"Car", "Hours", "Charge"};
for (int i = 0; i < 3; i++)
cout << data[i] << " ";
}
Obviously it is not using the array header, but it's a working example. If you do need to use the array header, you can try something like :
#include <array>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main() {
array<string, 3> ar3 = {"Car", "Hours", "Charge"};
cout << ar3.size() << endl;
for (auto i : ar3)
cout << i << ' ';
return 0;
}
You can see it working online here
I'm new to using MinGW and C++ in general, and whenever try compile my source file, the executable seemingly does nothing, even though it should output a simple "Hello World!". I've tried commenting out the lines that use vector, and it'd output "Hello World!" just fine.
Source code which doesn't seem to do or output anything:
#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
const int LIMIT = 1000000; // one million
int main(int argc, char *argv[])
{
int* arr = new int[LIMIT] {0};
vector<int>* v = new vector<int>();
cout << "Hello World!" << endl;
delete v;
return 0;
}
Source code with vector commented out that outputs "Hello World!" just fine:
#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
const int LIMIT = 1000000; // one million
int main(int argc, char *argv[])
{
int* arr = new int[LIMIT] {0};
// vector<int>* v = new vector<int>();
cout << "Hello World!" << endl;
// delete v;
return 0;
}
Both are compiled and run with:
C:\Users\User\my\directory\src> g++ source.cpp -o source.exe -std=c++11
C:\Users\User\my\directory\src> ./source.exe
Compiling and running either doesn't produce any error message. Note that #include <vector> remains uncommented in both instances.
I am using cygwin and I have boost 1.62.0 installed. I have compiled the following code using g++ and it functions as expected printing "Hi!" to the screen.
#include <iostream>
int main(){
std::cout << "Hi!" << std::endl;
}
When I try to compile the following code, I get nothing on the console. No "Hi!" and no printout of my vector 'v' that I created.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i) {
v (i) = i;
}
std::cout << "Hi!" << std::endl;
std::cout << v << std::endl;
}
Even inserting #include <iostream> at the top doesn't help. Why is this happening?
UPDATE: I created a new, simplified test case with the following code:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>
int main(){
std::cout << "Hi!" << std::endl;
//boost::numeric::ublas::matrix<double> m (3, 3);
}
This program prints "Hi!" as expected and gives the return code "0" when I run $ echo $ at the prompt, but as soon as I uncomment the commented line, it doesn't output anything and the return code is "127".
i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}
I want to define a vector with static content in a function that might be used by multiple programs. However I am facing some problems.
The main.cpp looked like this:
#include <iostream>
#include <boost/assign/std/vector.hpp>
using namespace std;
using namespace boost::assign;
int main (int argc, char* argv[]) {
vector< int > v;
v += 3,5,1;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
This works and I get as an output:
v: 0 3
v: 1 5
v: 2 1
However if I try to put the vector definition into a separate file it does not work.
main.cpp
#include <iostream>
#include "vector_test.hpp"
using namespace std;
int main (int argc, char* argv[]) {
vector< int > v;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
vector_test.hpp:
#include <boost/assign/std/vector.hpp>
#include <stdio.h>
using namespace std;
using namespace boost::assign;
static std::vector<int> v;
v += 3,5,1;
Trying to compile this gives me an error:
'v' does not name a type
and the qt creator also tells me:
expected a declaration
How do I fix this?
One way would be to have a function returning a reference to a static instance:
const std::vector<int>& get_magic_vector()
{
static std::vector<int> v{3, 5, 1};
return v;
}
Then
for (auto i : get_magic_vector())
std::cout << i << " ";
In your other files you must say that the variable exists but is defined in another file. That is, in otherFile.cpp you place the following line at file scope:
extern std::vector<int> v;