C++ :: Converting 1D Vector Into 2D Vector
Apr 20, 2013
Code:
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<double> > DV; //2d vector
std::vector<double>temp(8,0.0); //1d vector
[Code] .....
The conversion actually works but it does not give the expected the result. The output should be:
Code:
1 2 3
4 5 6
7 8
and it outputs:
Code:
123123123
View 5 Replies
ADVERTISEMENT
Aug 20, 2014
I want to convert the matrix to vector and vice verse.
View 1 Replies
View Related
Feb 15, 2012
I'm able to convert an integer to a vector<unsigned char> and back. However, when I try to use a nearly identical function designed for the long long data type, the last byte or two is broken.
Program code:
long long num = 9223372036854775551LL;
cout << "Before: " << num << endl;
vector<unsigned char> data = getBytes(num);
num = getLongLong(data);
cout << "After: " << num << endl;
Code for converting between vector<unsigned char> and long long:
Code:
vector<unsigned char> getBytes(long long value) {
int bytes = sizeof(value);
vector<unsigned char> data(bytes);
for (int i = 0; i < bytes; i++)
data.at(i) = (unsigned char)( value >> ((bytes-i)*8) );
[Code] ....
Output:
Code:
Before: 9223372036854775807
After: 9223372036854775552
Is there something special about long long that would prevent this from working? This problem doesn't happen with int.
View 3 Replies
View Related
Feb 18, 2015
I have saved the contents of an int vector to a txt file and the numerical data was converted into a c-string. Nov I need to import and read the contents back into my program but I have not been able to convert c-string numerical data back into ints.
View 1 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Mar 26, 2013
Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>
How do I make a function that creates vector of vector of every different integers?
<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>
and so on...
View 2 Replies
View Related
Jul 5, 2013
I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.
For instance, here is a test program that successfully finds the maximum according to just one member of the structure:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
[Code] ....
And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9
However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.
This was the description of the std::find function that I found: find - C++ Reference
Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference
Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:
Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;
Is there another std function that I can use to make search and find an element according to just one member of myStruct?
Or perhaps there is a clever way to pass two arguments to the unary predicate.
View 4 Replies
View Related
May 8, 2014
I am getting a string from the consle and that all works fine. I'm expecting a string of number, ie 123456.... I store that fine in my string object. I then need to take each individual number, ie 1, and put it in a vector then take the next number, ie 2, and put that in the next element in my vector.
Everything i've found and does eaither takes the entire string and stores it as an int, or takes the ascii representation of the number and stores that. How do I seperate each individual number and store them seperatly. below is a few different variations i've tried that doesn't produce my desired results.
cout<<"Please type the memory memory frames (numbers 0-9) to be used to simulate the input memory "<<endl;
cout<<"Your input: ";
string line="";
cin>>line;
cout<<"
Please wait while your input is processed and validated..."<<endl;
[code].....
the only thing i havn't tried yet is string streams and i don't necessarly like using them. Am i going to have to go that route? will that even work?
EDIT:I forgot to mention that this is being done on linux and then ported to unix.
View 7 Replies
View Related
Mar 31, 2014
I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:
void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);
[Code] ....
However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):
void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){
[Code] ....
The program crashes. Am I even printing the matrix correctly?
View 4 Replies
View Related
Mar 30, 2013
I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
struct InventoryItem
[Code] ....
View 4 Replies
View Related
Mar 24, 2012
Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
[Code]....
Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.
View 14 Replies
View Related
Jun 21, 2012
I am trying to copy vector to vector as follow:
Code:
std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;
Now I want to to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?
std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());
View 2 Replies
View Related
Sep 11, 2013
What is another name for a Vector inside of a Vector. What is the name of this construct? Would it be a Constructor Vector? I think this is a trick question my teacher is asking...
View 4 Replies
View Related
Apr 13, 2013
Simple rect collision detection
bool check_collision(SDL_Rect *box1, SDL_Rect *box2){
if(box1->x+box1->w<=box2->x)return false;
if(box1->x>=box2->x+box2->w)return false;
if(box1->y+box1->h<=box2->y)return false;
if(box1->y>=box2->y+box2->h)return false;
return true;
[Code] ...
The order of the game is of course standard.
1.handle input
2. handle logic
3. render
4. update
It's all pretty simple and straight forward, but oftentimes (not always) when the weapon hits the asteroid, the program crashes. If for example I turn off the delete main_weapon[i] and main_weapon.erase(main_weapon.begin()+i); the program works perfectly, but of course the bullets go right though the asteroid object, which is not what I want. It's only the weapon deletion which is causing no end of trouble.
View 7 Replies
View Related
Jun 2, 2014
I am writing a c++ program which uses a vector which contains rows and columns of a table.Unfortunately I got the following error:
IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=CCellDescr *, _Alloc=std::allocator<CCellDescr *>]" matches the argument list
argument types are: (const CCellDescr)
object type is: std::vector<CCellDescr *, std::allocator<CCellDescr *>>Here`s the code:
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<iterator>
using namespace std;
[code].....
View 1 Replies
View Related
Mar 15, 2013
how to use 2D vector instead of 2D arrays?
View 1 Replies
View Related
Feb 15, 2013
btw this is a part of my program
void query::load_query(const char* filename){
string lines;
int count = 0;
ifstream file (filename);
//READ OPERATION--ONE EXECUTION ONLY
if(file.is_open()) {
[Code]...
the 'flds' on the code above has vector <string> data type, i was able to output it using cout but i don't know how to copy its value to another vector <string>...whenever i tried to do that using my own way, the compiled program ended up crashing...
View 5 Replies
View Related
Mar 10, 2014
I rote this code in my program
JSONNode::const_iterator iter = root.begin();
for (; iter!=root.end(); ++iter) {
const JSONNode& arrayNode = *iter;
std::string type = arrayNode["type"].as_string();
if(type == "node")
[code]......
But when i try to ptint id of second item std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl; it gets a blank value. but size of the collection is 82, get the correct value for the collection size.
View 1 Replies
View Related
Mar 3, 2014
for example, I defined an empty 2D vector
vector<vector<size_t> > PI;
now I want to expand this 2D vector to Nx rows and Ny columns one by one and assign different value for each;
How can I do this? using push_back()?
View 1 Replies
View Related
Sep 30, 2014
My problem is that I have two classes that both need members from each other, so I need to use forward declaration + pointers, etc. However, trying to access a vector from a pointer throws an error.
Here's the code:
for(int j = 0; j < level->GetCollisionObjects().size(); j++){
if(fr.intersects(level->GetCollisionObjects()[j])){
rotate = false;
}
}
level is a pointer to class Level. This function is inside class GamePiece. Class Level includes class GamePiece, but GamePiece needs access to the vector CollisionObjects. This function throws a vector size error, and I'm totally lost on what to do.
View 3 Replies
View Related
Jan 30, 2013
I have a program that stores values in a map>. I use the map as a dictionary so that I can look through my map if a perticular string already exists. If a string does exist, I want to put the position that I found it at (relative to my text file) in my vector. How do I do this? So far I've tried this:
void mapInsert(string aWordNoSpaces, int index){
// check if word exists in map already, but first we need an iterator object
map<string,vector<unsigned int>>::iterator it;
it = wordMap.find(aWordNoSpaces);
if(it == wordMap.end()){
wordMap.insert(pair<string,vector<unsigned int>>(aWordNoSpaces,index));
[Code]...
Also, why are the zeros showing up here (i've attached a screenshot and circled them): [URL]...
View 5 Replies
View Related
Mar 9, 2014
I wrote this code in my program
JSONNode::const_iterator iter = root.begin();
for (; iter!=root.end(); ++iter) {
const JSONNode& arrayNode = *iter;
std::string type = arrayNode["type"].as_string();
if(type == "node")
[Code] .....
But when I try to ptint id of second item std::cout<<"Item Id ->>>>>>>>>>>>>" << collection[2].GetId() << std::endl;
it gets a blank value. but size of the collection is 82, get the correct value for the collection size.
View 1 Replies
View Related
Nov 3, 2014
I'm supposed to create a program that stores names and then the final function is supposed to remove a name. But it only removes the first name instead of the inputted name.
the problem with names.erase(names.begin()+i)?
#include <iostream>
#include <string>//include for string function
#include <vector>//include for vectors
[Code].....
View 9 Replies
View Related
Jun 10, 2014
This is what my code looks like
class Transaction{
private:
char type;
double amount;
double balance;
string description;
[Code] .....
This part is what I am having trouble with as this section is not printing anything.
for (int i = 0; i < transactions.size(); i++){
cout << transactions[i].gettype() << " ";
cout << transactions[i].getamount() << " ";
cout << transactions[i].getbalance() << " ";
cout << transactions[i].getdescription() << " ";
}
}
View 2 Replies
View Related