CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: concatenate int value from vector to long long int

  1. #1
    Join Date
    Dec 2015
    Posts
    48

    concatenate int value from vector to long long int

    how can i concatenate value from a int vector to long long int i found a way i don't know it is the good way or not
    Code:
    #include <iostream>
    #include <vector>
    
    
    using namespace std;
    
    
    int main(){
    
    	vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    
    	long long int g = 0;
    	const int Cn = 10;
    	int x = 0;
    	int szii = a.size();
    	for (int i = 0; i < szii; i++){
    
    		if (i == 0){
    
    			g = a[i];
    
    		}
    		else
    		{
    			x = a[i];
    			g = Cn * g + x;
    		}
    
    	}
    
    	return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by Ramees219
    how can i concatenate value from a int vector to long long int
    What exactly does that mean? Perhaps you should provide example input and expected output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: concatenate int value from vector to long long int

    I think something like this. Consider
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	const vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    	const int Cn = 10;
    
    	long long int g = 0;
    
    	for_each(a.begin(), a.end(), [&](int i) {g = Cn * g + i;});
    
    	cout << g << endl;
    
    	return 0;
    }
    This produces the same value as your original code.

    PS You might want to consider making g unsigned.
    Last edited by 2kaud; February 4th, 2016 at 02:01 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: concatenate int value from vector to long long int

    You should also consider using accumulate. It takes care of a few more details for you, such as not having to create a variable to accumulate into, or creating a capture in your lambda:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	const vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    	const long int Cn = 10;
    
    	long long int g =
    		accumulate(a.begin(), a.end(), 0, [](int seed, int value) {return Cn * seed + value;});
    
    	cout << g << endl;
    }
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Dec 2015
    Posts
    48

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by monarch_dodra View Post
    You should also consider using accumulate. It takes care of a few more details for you, such as not having to create a variable to accumulate into, or creating a capture in your lambda:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	const vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    	const long int Cn = 10;
    
    	long long int g =
    		accumulate(a.begin(), a.end(), 0, [](int seed, int value) {return Cn * seed + value;});
    
    	cout << g << endl;
    }
    Thanks for showing me a new way . But i wonder how its work long long int to a vector

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: concatenate int value from vector to long long int

    But i wonder how its work long long int to a vector
    Consider
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    
    int main()
    {
    	const vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    	constexpr int Cn = 10;
    
    	auto g = accumulate(a.begin(), a.end(), 0LL, [&](long long int seed, int value) {return Cn * seed + value;});
    
    	cout << g << endl;
    }
    g has type of long long int.

    Note that the lamda needs a capture for Cn.

    PS Also consider specifying the type of the vector and the type of the required value as
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;
    
    int main()
    {
    	using req_type = long long int;
    	using vec_type = int;
    
    	const vector<vec_type> a = { 4, 5, 8, 7, 4, 5, 1 };
    	constexpr int Cn = 10;
    
    	auto g = accumulate(a.begin(), a.end(), req_type(0), [&](req_type seed, vec_type value) {return Cn * seed + value;});
    
    	cout << g << endl;
    }
    Last edited by 2kaud; February 5th, 2016 at 09:18 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by 2kaud View Post
    Note that the lamda needs a capture for Cn.
    I don't know what the speck say exactly, but given that Cn is const and compile time known, I do not think you need to capture it. Rather, it gets "baked" into the lambda. The code you posted works just as well if you explicitly say "capture nothing" ("[]"). gcc accepts it anyways.

    True about the unsigned long thing.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: concatenate int value from vector to long long int

    gcc accepts it anyways.
    but VS2015 doesn't

    error C3493: 'Cn' cannot be implicitly captured because no default capture mode has been specified
    Last edited by 2kaud; February 6th, 2016 at 05:01 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  9. #9
    Join Date
    Jun 2015
    Posts
    208

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by 2kaud View Post
    error C3493: 'Cn' cannot be implicitly captured because no default capture mode has been specified
    VS2015 is right. [] means no capture at all. To get implicit capture you need to specify either [=] for value or [&] for reference.

    Capture by value is generally safer and there's never a reason to capture a constant primitive (like Cn) by reference anyway. So in this case [=] is better and the more specific [Cn] is even better still.

    But I would like to make another point.

    The OP may get the impression that there's something wrong with the explicit looping approach and that the standard function approach is inherently better. I don't agree. What's wrong with the plain range-based for-loop?

    Code:
    int main() {
    	vector<int> a = { 4, 5, 8, 7, 4, 5, 1 };
    	const int Cn = 10;
    
    	long long int g = 0;
    	for (auto x : a) {
    		g = Cn * g + x;
    	}
    
    	cout << g << endl;
    	return 0;
    }
    I'm heavily into functional programming so I'm no stranger to functions but in this case the use of a function just adds complexity and obscurity without offering any additional benefits.
    Last edited by tiliavirga; February 7th, 2016 at 07:29 AM.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,144

    Re: concatenate int value from vector to long long int

    the more specific [=Cn] is even better still
    With VS2015 this gives "error C2146: syntax error: missing ']' before identifier 'Cn'". Although [&Cn] is OK.

    What's wrong with the plain range-based for-loop?
    Yet another way! This though is probably the easier to read. I would be interested in guru's views regarding the performance of these various methods.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  11. #11
    Join Date
    Jun 2015
    Posts
    208

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by 2kaud View Post
    With VS2015 this gives "error C2146: syntax error: missing ']' before identifier 'Cn'". Although [&Cn] is OK.
    Sorry, it's a typo. I should be [Cn]. This means Cn is captured by value. I'll modify my post.

    Yet another way! This though is probably the easier to read. I would be interested in guru's views regarding the performance of these various methods.
    It's not just "yet another way". There's a huge qualitative difference where in my view the range-based for-loop is the outstanding winner in this particular example. It's much easier to understand and get right than the function approach where the increased code complexity adds an extra layer of potential mistakes.

    Regarding performance, I'd say an optimizing compiler most likely will generate pretty much the same code in both cases.
    Last edited by tiliavirga; February 7th, 2016 at 07:31 AM.

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by tiliavirga View Post
    VS2015 is right. [] means no capture at all. To get implicit capture you need to specify either [=] for value or [&] for reference.
    I'm not sure that is right, as Cn is never ODR-used to begin with, meaning it does not need to be captured at all. Clang also seems to accept it.

    I found this bug report, though it hasn't been accepted.
    https://connect.microsoft.com/Visual...expr-variables

    Finding more info on this is hard though :/

    EDIT: This seems like a good post about the issue:
    http://stackoverflow.com/questions/2...scope-variable
    Last edited by monarch_dodra; February 7th, 2016 at 02:45 PM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  13. #13
    Join Date
    Jun 2015
    Posts
    208

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by monarch_dodra View Post
    I'm not sure that is right, as Cn is never ODR-used to begin with, meaning it does not need to be captured at all. Clang also seems to accept it.
    Okay, VS2015 may be wrong then but this only emphasizes my point that in this case the function-with-lambda approach is far more convoluted than the for-loop approach.

    I think it's a good programming principle to use the simplest among functionally equal and equally efficient solutions and in this case the for-loop approach is the winner. The OP should know that there's no general consensus that a function always beats a loop.
    Last edited by tiliavirga; February 8th, 2016 at 03:39 AM.

  14. #14
    Join Date
    Oct 2008
    Posts
    1,456

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by monarch_dodra View Post
    I'm not sure that is right, as Cn is never ODR-used to begin with, meaning it does not need to be captured at all. Clang also seems to accept it.
    AFAIK, clang is right because const int's ( and unscoped enumerations ) initialized by a constant expression are like constant expressions themselves when converted to rvalues ( eg. if no address is taken ), so there's no implicit capture even if the variable is automatic.

    but note that this situation is quite brittle though

    Code:
    const int x = 123; // or constexpr, it's the same in this case ...
    // static const int x = 123; // this would always work, but would give a linker error if at class scope and ODR used
    
    []{ x+x; }; // OK
    []{ std::max(x,x); }; // error, needs default capture
    []{ std::max(+x,+x); }; // OK, converted to rvalue
    // ...

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: concatenate int value from vector to long long int

    Quote Originally Posted by superbonzo View Post
    AFAIK, clang is right because const int's ( and unscoped enumerations ) initialized by a constant expression are like constant expressions themselves when converted to rvalues ( eg. if no address is taken ), so there's no implicit capture even if the variable is automatic.

    but note that this situation is quite brittle though

    Code:
    const int x = 123; // or constexpr, it's the same in this case ...
    // static const int x = 123; // this would always work, but would give a linker error if at class scope and ODR used
    
    []{ x+x; }; // OK
    []{ std::max(x,x); }; // error, needs default capture
    []{ std::max(+x,+x); }; // OK, converted to rvalue
    // ...
    Those work for me though, as you can always pass a const expression to functions taking const refs.

    I can see it being an issue if you want to pass your x to a value taking by const pointer though.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)