C/C++ :: Swapping Vector Int And Vector Strings
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
ADVERTISEMENT
Apr 5, 2013
I have a file where the first column shows letters, second column shows numbers.
How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?
View 13 Replies
View Related
May 22, 2014
Alright I hav a program that readings from a txt file but is there a way to replace some of the words that get loaded into the vector so for example if the txt has a list of animals and i want to replace the word bird for book is their a way to do that
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
[Code]....
View 1 Replies
View Related
Jul 13, 2014
I'm having trouble storing a string in a vector.
I keep getting the errors in lines 51-54: no suitable constructor exist to convert from
#include <iostream>
#include <string>
#include <vector>
#include "Movie.h"
using namespace std;
vector <string> movieActors;
void promptForMovie(Movie & myMovie);
[code]....
View 6 Replies
View Related
Aug 30, 2014
I'm trying to write a program that reads in from a .txt file the movie title, rating, director, actors etc. All I want to do is to just sort movie titles alphabetically and print all its attributes(director, actors). I tried using sort( movies.begin(), movies.end()) method, but it doesn't work.
here's my code btw:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include "Movie.h"
[Code]...
View 2 Replies
View Related
Sep 1, 2014
I'm trying to create a database/search engine program and I'm having remove duplicate strings from a vector. I'm mostly just trying to make it so that if 2 or more movie have the same title in the database, it will remove the duplicates and just print out one copy of the movie console. I tried using the unique() command, but it doesn't seem to work.
code:
#include <iostream>
#include <string>
#include <vector>
[Code].....
View 2 Replies
View Related
Dec 6, 2012
I thought that C++0x made it possible for vectors to be initialized with an initializer list, such as:
Code: vector<vector<string> > vv {{"hello", "goodbye", ""}};
I tried this syntax in both VS 2010 & VS 2012 Express For Desktop, and I get the same error in both compilers:
compiler error: non-aggregates cannot be initialized with initializer list
To put the code above in context, I'm going to have a .txt file with hundreds of thousands of string arrays, in initializer list format, such as:
{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on
The first 3 strings in each array will be non-zero in length, and the last 3 strings in each array will be empty, as shown above.
I want to be able to cut and paste the arrays right into the declaration, either with:
string arrayOfArrays[0][6] = {{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on };
or
vector<vector<string> > vecOfVectors = {{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on };
I know I can do the first, but apparently the second declaration method with vectors won't work. I would like to work with vectors, but I'm not sure about the initialization. The .txt file will be what it is, so the initialization will have to be able to work with its 'array-like initializer' format.
View 10 Replies
View Related
Nov 22, 2013
How could I search a vector element with multiple strings
i.e. vector.at(i) = This is a test
How could I search that to look for the word "test" in that element?
View 7 Replies
View Related
Sep 25, 2012
I'm trying to do a binary search on a vector of strings and keep getting this error. This is the PhoneEntry header file with the class declaration:
Code:
using namespace std;
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include"phoneNumber.h"
[Code] .....
View 5 Replies
View Related
Mar 18, 2014
I have a vector of
strings.vector input;
bob
sam
bob
sammom
aardvark
money
aardvark
wanted
I need to remove the duplicates but each part of the vector corresponds to the next location. They are pairs.
ex. bob corresponds to the its definition, which is sam.
I need to keep the first instance, so keep bob and sam, but remove the second instance of bob, so remove bob and sammon. Only the first instance of the pair need to kept.
It doesn't matter if the sam and sammon don't match, all that matters is the first part of the pair.
The vector is already in alphabetical order. I can't use the algorithm library.
View 4 Replies
View Related
Feb 24, 2014
The code is supposed to take either an int or a string (and their respective vectors) and insert a given int or string into the vector in ascending order. My code works properly for ints, but it's having a problem with strings.
The order I get with the strings given is
penguin
banana
great
jungle
For some reason comparing penguin to banana/great doesn't give the expected result. The template attached only includes the function and the private vectors needed for the function.
template<class T>
class orderedList {
public:
void insert(const T& item);
private:
vector<T> list;
int total = 0;
[Code] ....
View 11 Replies
View Related
Jul 19, 2013
I have read that the Erase-remove idiom is the way to go. I have a rough understanding of how this works but am unsure whether I can implement a match-counter along with it.
For counting alone, I would use something like this:
Code:
std::vector<std::string> v; // contains duplicate strings in different elements
std::string term = "foo"; // search term, changing at runtime as well
unsigned int matches = 0;
for( auto e : v ) {
if( e == term ) {
[Code] .....
I'm not sure how (or if) I can combine the two things. That is, I don't know how to integrate a function comparing two (changing) strings into the remove_if() method. Nor do I know how to increment a counter during iteration.
The vector is extremely large, so speed is paramount. I think there are many other avenues for optimization, but decreasing the vector's size for each consecutive search could deliver a big speed boost for subsequent searches I imagine, as traversing it holds the biggest cost.
View 3 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
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 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
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
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