How to decide is it a string or not? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have an array of strings and I'd like to assort the elements by real datatype ("1" -> int; "abc" -> string; "1a" -> string) :
#include <iostream>
#include<vector>
int main() {
std::vector<std::string> number;
std::vector<std::string> str;
std::string arr[5] = {"1","1a","ab","10.1","a2"};
for(int i = 0;i<5;i++){
if(arr[i] /* is string */){
str.push_back(arr[i]);
}
else if(arr[i] /* is int */){
number.push_back(arr[i]);
}
}
return 0;
}
What is the simplest way?
Thanks!

If your goal is to only examine whether a string contains integer/decimal or not, you can try a solution with regexes:
if(std::regex_match(arr[i], std::regex("[-|+]?[0-9]*[.,]?[0-9]+")))
number.push_back(arr[i]);
else
str.push_back(arr[i]);
I'm assuming you're not taking overflow into account because you're only using strings, otherwise if you wanted to convert strings to numbers, you'd have to take that into account and use other solutions.

Related

What does 'dict' do in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I was looking at a solution for this problem:
Given a string s, find the length of the longest substring without repeating characters.
The following solution was posted, but I am having trouble understanding what dict does. I've tried looking for documentation in C++. However, I have not found anything. Can someone explain how it works, and where I can find documentation?
int lengthOfLongestSubstring(string s) {
vector<int> dict(256, -1);
int maxLen = 0, start = -1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
dict is just the name that was used for this vector<int>, first parameter is the the size of vector, second is value that should be assigned to all of its positions.
This is one of the possible ways to use its constructor, check the example on this page.

Returning position of int [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
The task is to return the leftmost position of given int A in larger int B (counting from 0)
A = 53, B = 1953786,
return: 2
I've found similar problem, but in that solution java function "indexOf" was used, is there a possibility to do it in other way?
The easiest way to do this would be to convert them both to std::string, and then you can use functions of std::string to find the index, similar to how you'd have indexOf for Java.
#include <iostream>
#include <string>
int main()
{
int a = 53;
int b = 513953786;
std::string number = std::to_string(b);
std::string numberToFind = std::to_string(a);
auto index = number.find(numberToFind);
std::cout << index;
}
This will return 4, since I moved the 53 to be at the 4th index to show it won't find the earlier 5 or 3 I added, per the advice of #Blastfurnace in the comments
What about using the c++ function: std::find()? You could then do something like this:
int getIndex(int a, int b){
std::string a_str = std::to_string(a);
std::string b_str = std::to_string(b);
return a_str.find(b_str);
}

C++ get variable from variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there another solution, please?
if(!http_piconpath && http_tpl)
{ http_piconpath = http_tpl; }
If not exist http_piconpath but exist http_tpl then assign the value from http_tpl to http_piconpath.
You provide very little information about what you are doing. Assuming that you use strings from your comment, the if statement you've got is not valid for strings, you'll see your compiler screaming that it can't convert string to bool. Below is a very basic example. Note that you must initialise http_piconpath, else it will have a garbage value and you won't know if its value is set or not.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string http_piconpath = "";
string http_tpl = "string";
if(http_piconpath == "" && http_tpl != "") {
http_piconpath = http_tpl;
}
cout << http_piconpath << endl;
return 0;
}
Supposing that both are pointers (of compatible types),
if(!http_piconpath) http_piconpath = http_tpl;
Or
http_piconpath = http_piconpath ? http_piconpath : http_tpl;
If picon is null, it gets the value of tpl; if both are null, nothing changes.

C++: Algorithm wrong output ( works on java ) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone help me with my code ? I'm trying to use different algorithm but it returns way to big numbers, when i use algorithm between /* */ it works perfect, anyone can see whats wrong with my new code ? (same on java works)
int* czynnikiPierwsze(int n)throw (string){
if(n<0){
string wyjatek1="Nie mozna rozlozyc ujemnej liczby";
throw wyjatek1;
}
int b=0;
while(n>2){
n=n/tab[n-2];
b++;
}
dzielniki=new int[b]();
int j=0;
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
j++;
}
/* int a=n;
int*dzielniki=new int[30]();
for(int j=0;j<n+a;j++){
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
break;
}
}*/
return dzielniki;
}
There's no chance your second while(n>2) loop runs even once, since the first loop only exited when that same condition was no longer true.

if statement doesn't work with function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}
void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill