473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to cast a vector to an int

175 100+
is it possible to cast a vector of strings to an integer?

I tried ...


Expand|Select|Wrap|Line Numbers
  1.  
  2.     vector<string> data;
  3.     string values;
  4.     ifstream in("pf.ppm");
  5.     ofstream out( "pf_sin.ppm");
  6.  
  7.  
  8.     while( in.good() )
  9.     {
  10.         in >> values;
  11.         data.push_back(values);
  12.     }
  13.  
  14.     int row = (int)data[6];
  15.     int column = (int)data[7];
  16.  
  17.  

the values in data are filled with values ... when I try this I get conversion errors when compiling. I've never tried to cast a vector, so, is it possible? if so, how can I do it? I appreciate any help.
Oct 1 '08 #1
3 7735
Man4ish
151 100+
Here you are casting the type of element of vector (string) to int . Treat it like simple conversion from string to int .By uisng atoi() function you can convert the string to int.

Regards
Manish
Oct 1 '08 #2
oler1s
671 Expert 512MB
is it possible to cast a vector of strings to an integer?
Technically yes, but it’s not a meaningful cast. Casting does not mean converting. Also, since this is C++, you’ll want to learn about using the C++ casts as opposed to the C cast. It’s not an optional topic to learn.

In any case, what you are really asking is how to convert a string to an integer. You’ll want to look up stringstreams for this. Use a stringstream to attempt to convert each string into an integer.
Oct 1 '08 #3
oler1s
671 Expert 512MB
Also, your look to read in value is problematic. First you have in >> values. Then you push back the value. Now, what you want is that you read in values until you run out of values. And the idea is that in.good() returns false. But wait. Look at in >> values. It attempts to read from the file. Are you guaranteed success? Could in >> values possibly fail? Could at that very moment, there be no value to read? If you say yes, then what do you do immediately afterwards?
Oct 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott Bates | last post by:
I am attempting to create a JAXB Object that returns the CollectionType as a List. In my schema file, I used the following: <jxb:globalBindings> collectionType="java.util.Vector"...
1
by: da Vinci | last post by:
I have been trying to post to .help all day but nothing is going through. Will try it here and hope it does.... I am trying to access a getData() method that simply returns a stored integer...
2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
1
by: DJ.precario | last post by:
Hi, I have a vector (myVector) of BaseClass objects. I store in it a DerivedClass object (where DerivedClass inherits from BaseClass). Later I want to get the last element in the vector and...
8
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
12
by: Raider | last post by:
Why it's impossible for compiler to implicitly cast vector<>::iterator to void*? I have: void f(void *, size_t); .... vector<int> v; v.reserve(n); f(v.begin(), n * sizeof(int));
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
2
by: vikasetrx | last post by:
any idea how we can cast from std::vector types. e.g.. i am using the win32 API RegQueryValueEx(). the out buffer it takes should be LPBYTE. i used the code as shown, TCHAR * buf = new...
4
by: neisan | last post by:
Hi, I created a vector like that" typedef std::vector<Client*> Clients" and declared a vector "Clients clients". I specialized the Client class to SpecialClient, so I have: class Client ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.