Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to convert a fixed-length string array to a fixed-length integer array in C++

2 Answers

0 votes
#include <iostream>
#include <algorithm>

#define LEN 8

int main() {
    char str[LEN] = "abcdefg";
    int arr[LEN] = { 0 };
    
    std::copy(str, str + LEN, arr);
    
    for (int i = 0; i < LEN; i++) {
        std::cout << arr[i] << " ";
    }
}

  
/*
run:
 
97 98 99 100 101 102 103 0 
  
*/

 



answered Mar 18, 2025 by avibootz
0 votes
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>

#define LEN 4

template <class T>
T parse(const std::string& str) {
    T temp;
    std::istringstream iss(str);
    iss >> temp;
    if (iss.bad() || iss.fail()) {
        std::cout << "istringstream error";
    }
    
    return temp;
}


int main() {
    std::string foo[LEN] = {"97", "98", "99", "100"};
    int arr[LEN];

    std::transform(foo, foo + LEN, arr, parse<int>);
    
    for (int i = 0; i < LEN; i++) {
        std::cout << arr[i] << " ";
    }
}

  
/*
run:
 
97 98 99 100 
  
*/

 



answered Mar 18, 2025 by avibootz
...